request.file
给出以下统计数据,您只需从中选择request.file.originalname
或request.file.filename
获取nodejs app创建的新文件名.
{ fieldname: 'songUpload', originalname: '04. Stairway To Heaven - Led Zeppelin.mp3', encoding: '7bit', mimetype: 'audio/mp3', destination: './uploads', filename: 'songUpload-1476677312011', path: 'uploads/songUpload-1476677312011', size: 14058414 }
例如,在nodejs中用ecma-6表达mvc app,
var Express = require('express'); var app = Express(); var multipartUpload = Multer({storage: Multer.diskStorage({ destination: function (req, file, callback) { callback(null, './uploads');}, filename: function (req, file, callback) { callback(null, file.fieldname + '-' + Date.now());}}) }).single('songUpload'); app.post('/artists', multipartUpload, (req, resp) => { val originalFileName = req.file.originalname console.log(originalFileName) }
var express=require("express"); var app=express(); var multer=require("multer"); var upload=multer({dest:"uploads/"}); app.post("/multer", upload.single("file"), function(req,res){ console.log(req.file.filename); });
在Multer中,访问上传文件的数据有所不同,具体取决于您要上传单个文件还是多个文件。像这样访问数据:
上传单个文件:
req.file
上载多个文件:
req.files