1

I am trying to use multer to upload file

I use latest multer (v.0.18)

I configure the app this way

  var express = require('express')
                , multer = require('multer')
                , app = express() 
                , routes = require('./routes');

  app.use(multer({ dest: './uploads/',
       rename: function (fieldname, filename) {
       return filename+Date.now();
      },
        onFileUploadStart: function (file) {
        console.log(file.originalname + ' is starting ...')
      },
        onFileUploadComplete: function (file) {
        console.log(file.fieldname + ' uploaded to  ' + file.path)
        done=true;
      }
 }).single('photo'));

and it keeps returning "Undefined is not a function" (referring to the .single() )

if I remove the .single to multer({ dest: 'uploads/' });

this error message is gone, but of course the file itself becomes undefined then nothing is uploaded.

Work on it a day long, man! obviously need help :'(

dhayyati
  • 49
  • 9

2 Answers2

1

This is how I use multer, hope this helps you: (Create a directory and add the pictures to it with every POST request).
This is in the routes.js (or any name of the file you posted the code of):

var storage = multer.diskStorage( {
    destination: function (req, file, cb) {
        var destFolder =__dirname + '/../uploads/' + req.params.id;
        //create folder if does not exists
        if (!fs.existsSync(destFolder)) {
            fs.mkdirSync(destFolder);
        }

        //set folder where files will be populated
        cb(null, destFolder)
    },
    filename: function (req, file, cb) {
        //set the names file within the folder
        //as the original name of the file
        cb(null,file.originalname);
    }
});

And then I use a service (fileUploadService.js) to call it like so:

app.service('fileUpload', ['$http', function ($http) {
        this.uploadFileToUrl = function (files, uploadUrl) {

            var fd = new FormData();
            for (var i = 0; i < files.length; i++) {
                fd.append('file', files[i]);
            }

            $http.post(uploadUrl, fd, {
                transformRequest: angular.identity,
                headers: { 'Content-Type': undefined }
            })
            .success(function () {
                console.log("images files upload success");
            })
            .error(function () {
                console.log("images files upload fail");
            });
    }
}]);

This works perfectly for me, hopefully you can adapt it too. I understand the frustration since it also took me some time to get right.

Idos
  • 15,053
  • 14
  • 60
  • 75
1

You have an old version installed. The API you're trying to use was only introduced in recent versions (probably starting with v1.0.0).

You might check that your package.json isn't restricting what version of multer gets installed.

I should also point out that you will need to modify your multer options a bit when you upgrade: onFileUploadStart and onFileUploadComplete no longer exist and rename is now filename (but with a different, async function signature).

mscdex
  • 104,356
  • 15
  • 192
  • 153
  • I have installed version 1.1.0 now. It stops saying "undefined is not a function. However, when I do JSON.stringify(req.file), it printed "undefined" – dhayyati Dec 23 '15 at 06:55
  • Then most likely something is wrong with your request, for example: the `name` attribute of the file input is either wrong or missing completely, or the `Content-Type` (`enctype` form attribute) is not `multipart/form-data`. – mscdex Dec 23 '15 at 10:28