43

I'm just starting learning NodeJS and I am stuck with a problem. I would like to upload files to my server. To do so I searched and found out this module multer. Doing as the example on GitHub works:

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

var app = express()

app.post('/uploadImage', upload.single('image'), function(req, res) {
    console.log(req.file);
});

On posting an image with FormData to /uploadImage the image is saved in the uploads/ directory. The thing is the image is saved with a strange name and I would like to save it with its original name. To do so I understood that I would have to call app.use(multer({ dest: 'uploads/' }))' and then I would be able to access req.file in my function like:

app.post('/uploadImage', function(req, res) {
    console.log(req.file);
});

But I get an error on trying app.use():

TypeError: app.use() requires middleware functions
    at EventEmitter.use (project\node_modules\express\lib\application
.js:209:11)

Im using NodeJS 0.12.7 and Express 4.13.1

How can I achieve that upload? Thanks.

João Menighin
  • 3,083
  • 6
  • 38
  • 80
  • 1
    http://stackoverflow.com/a/31495796/4989460 – stdob-- Jul 18 '15 at 22:19
  • Oh my... I swer I have searched here... Thanks stdob... But well, strangely (for me) it stills uploads the file automatically that way... I though I would be able to control and write it with `fs`... Ill find a way, thx – João Menighin Jul 18 '15 at 22:28

7 Answers7

62

You need to use app.use(multer({dest:'./uploads/'})) in the form of one of these:

app.use(multer({dest:'./uploads/'}).single(...));
app.use(multer({dest:'./uploads/'}).array(...));
app.use(multer({dest:'./uploads/'}).fields(...));

ie:

app.use(multer({dest:'./uploads/'}).single('photo'));

And be sure to have something like:

<form action="/postPhotos" enctype="multipart/form-data" method="post">
    <input type="file" name="photo">
    <input type="submit" value="Upload photo">
</form>

In your html.

daparic
  • 3,794
  • 2
  • 36
  • 38
Ruben Marrero
  • 1,392
  • 1
  • 10
  • 23
23
var app = require('express');

var multer = require('multer');

app=express();

app.use(multer({dest:__dirname+'/file/uploads/'}).any());

app.post('/upload',function(req,res){

    console.log(req.files);

    res.redirect('/');

});
baldr
  • 2,891
  • 11
  • 43
  • 61
  • 5
    This answer would be improved if it contained some explanation of how it answers the question and solves the problem. – William Price Mar 15 '16 at 20:28
  • StackOverflow requests that you post questions, comments, and answers in English only, please. (En inglés, por favor.) Also, use the [edit] link to update your answer directly. – William Price Mar 15 '16 at 21:40
12

The answer from @127.0.0.1 is correct, but in case you are using Express Router, the code changes a little bit:

var express = require('express');
var multer = require('multer');

var router = express.Router();

var uploads = multer({
  dest: 'public/uploads/'
});

router.post('/upload', uploads.single('avatar'), function(req, res, next) {
  console.log(req.file);

  //...
});

And the important bit, the form encoding should be enctype="multipart/form-data" as already said, like that:

<form action="/upload" enctype="multipart/form-data" method="post">
    <input type="file" name="avatar">
    <input type="submit" value="Go avatar go!">
</form>
Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
  • Hi, I'm using `multer, expres, ng2-file-upload` stack and with your exactly code doesn't works for me. The req.file is undefined and the info of my file is on req.files(on my html I dont have the multiple selector). When I click to upload on my button the server return file uploaded bt the file is not present on server. What would be doing wrong? – Hanzo May 26 '17 at 11:01
  • I would suggest testing it without angular first. I mean sending the request to the backend directly with Postman or CURL or your favorite tool. If that works, the problem is the frontend. If it doesn't, the problem is the backend. It is not going to solve the problem, but it will help you solve it faster. – Salvatorelab May 26 '17 at 11:42
  • If I add this `app.use(multer({storage:storage}).single('file'));` works fine but not as expected with callbacks. Could you check this question? https://stackoverflow.com/questions/44165037/files-not-uploading-with-ng2-file-upload – Hanzo May 26 '17 at 11:49
  • 1
    Salvatorelab, I've been searching for 2 hrs+ trying to figure out why the I couldn't debug ``upload.single`` is not a function while use express router. Just want to point that out since that's not what the OP was trying to solve, might help someone else. – Paul W Dec 05 '20 at 04:29
7

You can't change the file name using multer but you can use Node.js stream and fs module to copy the content of already uploaded file to new file(set as original file name) on same folder and delete old one.

First of all import fs, path and multer in your node script.

var express = require('express');
var multer = require('multer');
var fs = require('fs');
var pathModule = require('path');

Now, set destination directory of any type of files using multer as below.

var app = express();
app.use(multer({dest:__dirname+'/resoucres/'}).any());

Now use stream and fs to resolve your issue.

app.post('/uploadImage', function(request, response) {
    var readerStream = fs.createReadStream(request.files[0].path);
    var dest_file = pathModule.join(request.files[0].destination, request.files[0].originalname);
    var writerStream = fs.createWriteStream(dest_file);

    var stream = readerStream.pipe(writerStream);
    stream.on('finish', function(){
        fs.unlink(request.files[0].path);
    });
});
Aditya
  • 1,214
  • 7
  • 19
  • 29
6

Since version 1.0.0

var upload = multer({ dest: 'tmp/' });
app.post('/file_upload', upload.single('photo'), function (req, res) {
josliber
  • 43,891
  • 12
  • 98
  • 133
Jacek Wojcik
  • 1,223
  • 2
  • 19
  • 33
4

Changing to app.use(multer({dest:'./uploads/'}).single('photo')); in the app.js works for me to start the server

2

I encountered the same problem. And I solved it. For the first problem, how to get the original file name? I printed the whole request and found we could get original name by using the path "req.file.originalname"

and the other question, how to use "app.use()"?
refers: here

Community
  • 1
  • 1
Haifeng Fu
  • 23
  • 3