Uploading a file and passing an optional parameter using multer

I use jQuery Form Plugin and multer to upload files to my server. This works fine, but I'm trying to pass an additional parameter that determines where the file will be saved.

I have the following code that I would like to extend to behave as indicated:

HTML

 <form id="uploadForm" enctype="multipart/form-data" action="/files" method="post"> <input type="file" id="userFile" name="userFile"/> <input type="text" hidden id="savePath" name="savePath" value="path"/> </form> 

JS Client

 uploadForm.submit(function() { $(this).ajaxSubmit({ error: function(xhr) { console.log('Error: ' + xhr.status); }, success: function(response) { console.log('Success: ' + response); } }); return false; }); 

Node.js Route

 app.post(APIpath + "file",function(req,res){ var storage = multer.diskStorage({ destination: absoluteServePath+ "/" + config.filePath, filename: function (req, file, cb) { cb(null, file.originalname); } }); var upload = multer({ storage : storage}).any(); upload(req,res,function(err) { if(err) { console.log(err); return res.end("Error uploading file."); } res.end("File has been uploaded"); }); }); 

Note: I know that I am not checking mimetypes or sanitizing user files, but basically this is secondary to me right now.

+5
source share
1 answer

The problem is that the multer , which first saves the files and then writes to req rest of the form - for example, hidden fields.

Try the following:

 app.post(APIpath + "file",function(req,res){ var storage = multer.diskStorage({ destination: tmpUploadsPath }); var upload = multer({ storage : storage}).any(); upload(req,res,function(err) { if(err) { console.log(err); return res.end("Error uploading file."); } else { console.log(req.body); req.files.forEach( function(f) { console.log(f); // and move file to final destination... }); res.end("File has been uploaded"); } }); }); 
+11
source

All Articles