Uploading multiple files using multer, but from different fields?

How can I accept files from several file type fields?

I have the following code that uploads a single file using multer in node.js:

var storage = multer.diskStorage({ destination: function (req, file, callback) { callback(null, './public/uploads'); }, filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now()); } }); var upload = multer({ storage : storage }); app.post('/rest/upload', upload.array('video', 1), function(req, res, next){ ... } 

From the following form, provided that only the video field matters (if I specify how I get the "Unexpected field" error):

 <form action="/rest/upload" method="post" enctype="multipart/form-data"> <label>Video file: </label> <input type="file" name="video"/> <label>Subtitles file: </label> <input type="file" name="subtitles"/> <input type="submit"/> </form> 

From the documentation it is not clear how to approach this? We appreciate any suggestions. BTW I tried the following options for options, without success:

 app.post('/rest/upload', [upload.array('video', 1), upload.array('subtitles', 1)] ... app.post('/rest/upload', upload.array('video', 1), upload.array('subtitles', 1), ... app.post('/rest/upload', upload.array(['video', 'subtitles'], 1), ... 
+8
source share
3 answers

Do you want upload.fields() :

 app.post('/rest/upload', upload.fields([{ name: 'video', maxCount: 1 }, { name: 'subtitles', maxCount: 1 }]), function(req, res, next){ // ... } 
+19
source

You tried to use multer (). any () ?

+2
source

Using Multer to upload files from two fields of separate forms on different pages In this example, I have two fields - a resume and an image. Summary in one form and image in another. Both are on separate pages. The first import dependencies

 const path = require('path'); // for getting file extension const multer = require('multer'); // for uploading files const uuidv4 = require('uuidv4'); // for naming files with random characters 

Define fileStorage and fileFilter const fileStorage = multer.diskStorage ({destination: (req, file, cb) => {// set the destination for storing the file

  if (file.fieldname === "resume") { // if uploading resume cb(null, 'resumes'); } else { // else uploading image cb(null, 'images'); } }, filename: (req, file, cb) => { // naming file cb(null, file.fieldname+"-"+uuidv4()+path.extname(file.originalname)); } }); const fileFilter = (req, file, cb) => { if (file.fieldname === "resume") { // if uploading resume if ( file.mimetype === 'application/pdf' || file.mimetype === 'application/msword' || file.mimetype === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ) { // check file type to be pdf, doc, or docx cb(null, true); } else { cb(null, false); // else fails } } else { // else uploading image if ( file.mimetype === 'image/png' || file.mimetype === 'image/jpg' || file.mimetype === 'image/jpeg' ) { // check file type to be png, jpeg, or jpg cb(null, true); } else { cb(null, false); // else fails } } }; 

Middleware for Multer

 app.use( multer( { storage: fileStorage, limits: { fileSize:'2mb' }, fileFilter: fileFilter } ).fields( [ { name: 'resume', maxCount: 1 }, { name: 'image', maxCount: 1 } ] ) ); 

And then name your routes. You may need to add security or csrf authentication along with this for security. But that should work fine.

0
source

Source: https://habr.com/ru/post/1245361/


All Articles