0

I am trying to implement file uploads with node.js and the multer middleware, but it doesn't seem to work. This is my code:

var express = require('express');
var multer = require('multer');
var done = false;
var app = express();

app.use(multer( {dest:'./uploads/',
            onFileUploadStart : function(file){
                console.log('File recieved:');
                console.log(file);
            },
             onFileUploadData:function (file,data){
                console.log('Data recieved');
            },
             onParseEnd: function(req,next){
                next();
             }
            }));

app.use(express.static(__dirname+"/public"));

app.post('/upload',require(__dirname+'/upload.js').upload);

app.listen(3000);

My form looks like this:

<html>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name ="file">
    <input type="submit" value="Upload selected file to server">
</form>    
</body>
</html>

And upload.js looks like this:

exports.upload = function (req,res)
{
   console.dir(req.files);
};

I think the problem is that my form is being submitted with "application/x-www-form-urlencoded" in the Content-Type header instead of "multipart/form-data", since this is what appears when I use Fiddler to monitor the request, but I have no idea why. Can anyone shed any light on this?

tutiplain
  • 1,427
  • 4
  • 19
  • 37

3 Answers3

1

I got it working by adding an accept attribute to my tag in my html. I don't know why in some examples this is not used.

Here's the code for my entire form:

<form action="/upload" method="post" enctype="multipart/form-data"> 

    <input type="file" name ="file" accept="application/x-zip-compressed,image/*"> 

    <input type="submit" value="Upload selected file to server"> 

</form>

Check the entire project eventually: https://github.com/tutiplain/quickupload

nbro
  • 15,395
  • 32
  • 113
  • 196
tutiplain
  • 1,427
  • 4
  • 19
  • 37
  • 1
    Here's the code for my entire form:
    – tutiplain Jun 24 '15 at 03:36
  • I'm not able to make this work. I've taken your entire source, but the log statements within multer don't fire and the "req.files" is always undefined – Asim Mittal Aug 28 '15 at 07:18
  • Use the project on github: https://github.com/tutiplain/quickupload. I've tested this on an Ubuntu server running in Azure and has worked like a charm. – tutiplain Aug 29 '15 at 20:04
  • Note that because of the accept attribute, it might only upload images and zip files. Haven't tested uploading other stuff yet. – tutiplain Aug 29 '15 at 20:06
0

I can see you are doing everything right. I used multer sometime back, you can look into my working implementation here. In this EJS file i have an image upload functionality and then i wrote some logic to store the file to some other location.

Make sure you have appropriate router, for example you can use router.post(..)

exports.upload= function(req,res){
    // get the temporary location of the file
    var tmp_path = req.files.file.path;
    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = '/..provide path to store photos../' + req.files.file.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) {
                throw err;
            }else{
              //response logic ...
             };
            });
        });
  };
NarendraSoni
  • 2,210
  • 18
  • 26
  • In my upload.js, req.files is equal to an empty object {}. And none of the console.log() calls inside the multer event handlers get called. As far as I can see, the request is routed properly, because the console.logs inside upload.js are displaying, but multer seems to be doing nothing, like the code was commented out. I guess it could be because the
    is not being sent as "multipart/form-data". But I have no idea why.
    – tutiplain Jan 12 '15 at 13:41
  • Could you push your whole project to either Github or any other repo. Let me have a look at it. I don't see any reason for it to not work. – NarendraSoni Jan 13 '15 at 06:29
0

You can try this. It works for me. If any issues then let me know

var multer  = require('multer');
var upload = multer({ dest: './uploads' });

router.post('/register',upload.single('profileimage'),function(req,res,next){

    if (req.file) {
        console.log('Uploading File');
        var profileImageOriginlName=req.file.originalname;
        var profileImageName=req.file.name;
        var profileImageMime=req.file.mimetype;
        var profileImagePath=req.file.path;
        var profileImageExt=req.file.extension;
        var profileImageSize=req.file.size;
    }
    else
    {
        var profileImageName='noimage.png';
    }

});
cchapman
  • 3,269
  • 10
  • 50
  • 68
Yadjosan
  • 3
  • 4