Node.js: file upload using multer in sails.js

I would like to integrate multer

https://www.npmjs.com/package/multer a file upload package in my application sails.js

, unfortunately there is no tutorial available for it.

Can anyone provide directions on how to do this in sails.js ??

+4


source to share


2 answers


I also ran into this problem and couldn't find anything on the internet, but after reading the documentation, sails.js

I have a sails.js

way to download the files.

Internally sails.js

uses skipper

upload files and codes are very easy for him.

var uploadFile = req.file('user_picture');
uploadFile.upload({ dirname: filePath }, function onUploadComplete(err, files) {
  if (err) return res.serverError(err);
  //  IF ERROR Return and send 500 error
  console.log(files);
});

      



user_picture

is the name of the variable in your form.

Hope this helps!

0


source


module.exports.http = {
  middleware: {
    /***************************************************************************
    * *
    * The order in which middleware should be run for HTTP requests. *
    * (This Sails app routes are handled by the "router" middleware below.) *
    * *
    ***************************************************************************/
    order: [
      'cookieParser',
      'session',
      'fileMiddleware',
      'bodyParser',
      'compress',
      'poweredBy',
      'router',
      'www',
      'favicon',
    ],
    /***************************************************************************
    * *
    * The body parser that will handle incoming multipart HTTP requests. *
    * *
    * https://sailsjs.com/config/http#?customizing-the-body-parser *
    * *
    ***************************************************************************/
    bodyParser: (function () {
      const bodyParser = require('body-parser');
      bodyParser.json();
      bodyParser.urlencoded({ extended: true });
      return bodyParser();
    })(),
    fileMiddleware: (function () {
      const multer = require('multer');
      const upload = multer();
      return upload.fields([{ name: 'images' }, { name: 'profile' }, { name: 'manual' }, { name: 'cover' }, { name: 'newsMedia' }, { name: 'vrzip' }]);
    })(),
  }
};

      



You can try this. This will replace the skipper.

0


source







All Articles