前台: <form method="post" enctype="multipart/form-data" action="/upload"> <input type="file" name="thumbnail"> <input type="submit"> </form> app.js代码: var express = require(‘express’); var multer = require(‘multer’); var upload = multer({dest:’./public/uploads’}); var app = express(); app.use(upload); app.post(’/upload’,upload.single(‘avatar’),function(req,res,next){ console.log(req.file); res.send(‘ok’); }); multer版本1.1.0 哪位高手有可用的上传代码欢迎贴出来!,谢谢啦!
@jingsam 谢谢回答,删掉之后报这个错了
Unexpected field
Error: Unexpected field at makeError (D:\nodejsWorkspace\nodejs-demo\node_modules\multer\lib\make-error.js:12:13) at wrappedFileFilter (D:\nodejsWorkspace\nodejs-demo\node_modules\multer\index.js:39:19) at Busboy.<anonymous> (D:\nodejsWorkspace\nodejs-demo\node_modules\multer\lib\make-middleware.js:112:7) at emitMany (events.js:108:13) at Busboy.emit (events.js:182:7) at Busboy.emit (D:\nodejsWorkspace\nodejs-demo\node_modules\multer\node_modules\busboy\lib\main.js:31:35) at PartStream.<anonymous> (D:\nodejsWorkspace\nodejs-demo\node_modules\multer\node_modules\busboy\lib\types\multipart.js:213:13) at emitOne (events.js:77:13) at PartStream.emit (events.js:169:7) at HeaderParser.<anonymous> (D:\nodejsWorkspace\nodejs-demo\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\Dicer.js:51:16)
你有上传成功的代码示例吗?
@jingsam 你好,还有个问题,①如果用表单提交的方式上传,那<input type="file" name="avatar">这个name如何变成自动获取所上传文件的名字?② upload.single(‘avatar’),这个后台怎么变成取表单传过来的名字?③除了用表单提交的方式,用http的post请求上传文件怎么写?蟹蟹!