Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expressjs file upload, check whether there was indeed a file sent, specify max filesize & keep its name

Working with expressjs for about a month by now I have stumbled across the problem of file uploads. Despite consulting Google & various blogs I have failed finding an answer to the following three questions:

What do I have to do / what settings for bodyParser do I have to choose in order to:

  1. Make sure there was indeed a file uploaded (currently, when submitting the form without choosing a file an empty file gets created).

  2. Where can I specify a value for the maxium size a file is allowed to have?

  3. How can I omit the renaming of the file?

Currently I am including bodyParser in my express (v. 3.0.0) app with the following option:

{keepExtensions: true, uploadDir: __dirname + '/public/uploads'}
like image 698
R.G. Avatar asked Aug 03 '12 15:08

R.G.


People also ask

How do I fix 413 payload too large Express?

To fix the “413 Request Entity Too Large” error in an Express application, you only have to update the server initialization file. This is generally called index. js or server. js and is where you initialize the Express server.

How to handle multer error?

Error handling If you want to catch errors specifically from Multer, you can call the middleware function by yourself. Also, if you want to catch only the Multer errors, you can use the MulterError class that is attached to the multer object itself (e.g. err instanceof multer.

Why multer is used?

Multer is a node. js middleware for handling multipart/form-data, which is primarily used for uploading files.

What is multer Express?

Multer is a middleware designed to handle multipart/form-data in forms. It is similar to the popular Node. js body-parser , which is built into Express middleware for form submissions. But, Multer differs in that it supports multipart data, only processing multipart/form-data forms.


2 Answers

I recently ran into a similar problem. for Question #2, check http://www.senchalabs.org/connect/middleware-limit.html

app.use(express.limit('4M')); // in your app.configure()

app.post('/upload', function(req, res) {
    var fs = require('fs'),
        file = req.files.myfile; //your file field;


    if(file.size === 0) { // question #1
        fs.unlinkSync(file.path);
        res.redirect('/error?');
    } else { //question #3
        var fn = file.path.split('/');
        fs.rename(file.path, file.path.replace(fn[fn.length-1], file.name));
        res.redirect('/success?');
    }
});
like image 138
OneOfOne Avatar answered Sep 23 '22 07:09

OneOfOne


About your concern

Make sure there was indeed a file uploaded (currently, when submitting the form without choosing a file an empty file gets created).

After long search I found a great solution

First you need to install node-formidable

Then after including the library, to check if file uploaded in the first place

var form = new formidable.IncomingForm();

form.parse(req, function(err, fields, files) {
  if (Object.keys(files).length !== 0) {
    console.log("File exists")
  } else {
    console.log("File does not exist")
  }
});

And about the size you can check this.

form.maxFieldsSize = 2 * 1024 * 1024;

Best regards.

Abdulaziz Noor

like image 28
Abdulaziz Noor Avatar answered Sep 23 '22 07:09

Abdulaziz Noor