tutiplain
tutiplain

Reputation: 1479

node.js upload with multer not working

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?

Upvotes: 0

Views: 10829

Answers (3)

Yadjosan
Yadjosan

Reputation: 3

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';
    }

});

Upvotes: 0

tutiplain
tutiplain

Reputation: 1479

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

Upvotes: 1

NarendraSoni
NarendraSoni

Reputation: 2258

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 ...
             };
            });
        });
  };

Upvotes: 0

Related Questions