Downloading files using Multer without knowing their field name

Having looked at this article: http://lollyrock.com/articles/express4-file-upload/

I realized that Multer used to upload files when you did not know what name is being loaded in the form field. For example, if you look at the โ€œUsing Multerโ€ section of the article, youโ€™ll see that when calling app.use() author does not use .single() , .array() or .fields() . If you do this with the current version of Multer, you will receive a TypeError: app.use() requires middleware functions error TypeError: app.use() requires middleware functions .

While I have a little idea on how to use .single() , .array() or .fields() , my current project requires me to send a non-standard number of files to the server (maybe a series of .png or .log ). Therefore, I do not know what field names will be in advance.

This was easy using the version of Multer used in the article (0.1.6), but it seems impossible when trying to use it in the current version of Multer (1.0.3), since you need to specify the names of the form fields.

As an alternative, finding a complete guide for an online cartoon was a difficult task, since the best of them seems to be the Readme from the GitHub repo, and this seems to be missing. Perhaps the answers I'm looking for will be somewhere in the guidebook.

Thanks!

+4
source share
4 answers

Just use multer.any () and you will get these files in request.files.

 var router = express.Router(); var app = express(); var _fs = require("fs"); var _config = require("./config"); var multer = require("multer"); var upload = multer({ dest: _config.tempDir }) app.use(bodyParser.urlencoded({extended:true})); app.post("/api/files", upload.any(), function(req, res){ var files = req.files; if(files){ files.forEach(function(file){ //Move file to the deployment folder. var newPath = _utils.DetermineFileName(file.originalname, _config.destinationDir); _fs.renameSync(file.path, path.join(_config.destinationDir, newPath)); var newFileName = path.basename(newPath); console.log(newFileName + ' saved at ' + _config.destinationDir ); }); } }; 
+5
source
 multer().any(); 

Did not test it, but it should work.

+1
source

Just use .array ('name'), now you can get as many files that have the same field as the name, also define this name on your client side as arguments. I use the same approach, and it works great, if there is still any request you receive, feel free to comment :)

0
source

Hacking bit, but works with Multer v1.1.0:

 multer().single("undefined") 
0
source

All Articles