节点Multer意想不到的领域

我正在使用multer npm模块将file upload到我的应用程序。

我定义的multerfunction是允许单个file upload到文件系统。 在运行期间一切正常; 问题是我上传文件后,我得到一个错误如下。 任何build议赞赏在哪里看。

错误:

Unexpected field Error: Unexpected field at makeError (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\lib\make-error.js:12:13) at wrappedFileFilter (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\index.js:39:19) at Busboy.<anonymous> (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\lib\make-middleware.js:97:7) at Busboy.emit (events.js:118:17) at Busboy.emit (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\node_modules\busboy\lib\main.js:31:35) at PartStream.<anonymous> (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\node_modules\busboy\lib\types\multipart.js:205:13) at PartStream.emit (events.js:107:17) at HeaderParser.<anonymous> (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\Dicer.js:51:16) at HeaderParser.emit (events.js:107:17) at HeaderParser._finish (c:\Users\Dev\WebstormProjects\Crunch\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:70:8) 

app.js

 var multer = require('multer'); var app = express(); var fs = require('fs'); //. . . var upload = multer({ dest: 'upload/'}); var type = upload.single('file'); app.post('/upload', type, function (req,res) { var tmp_path = req.files.recfile.path; var target_path = 'uploads/' + req.files.recfile.name; fs.readFile(tmp_path, function(err, data) { fs.writeFile(target_path, data, function (err) { res.render('complete'); }) }); 

Index.hbs

 <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name='recfile' placeholder="Select file"/> <br/> <button>Upload</button> </form> #Package.json "dependencies": { "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", "easy-zip": "0.0.4", "express": "~4.13.1", "hbs": "~3.1.0", "less-middleware": "1.0.x", "morgan": "~1.6.1", "multer": "~1.0.0", "serve-favicon": "~2.3.0" } } 
 var multer = require('multer'); var upload = multer({ dest: 'upload/'}); var fs = require('fs'); /** Permissible loading a single file, the value of the attribute "name" in the form of "recfile". **/ var type = upload.single('recfile'); app.post('/upload', type, function (req,res) { /** When using the "single" data come in "req.file" regardless of the attribute "name". **/ var tmp_path = req.file.path; /** The original name of the uploaded file stored in the variable "originalname". **/ var target_path = 'uploads/' + req.file.originalname; /** A better way to copy the uploaded file. **/ var src = fs.createReadStream(tmp_path); var dest = fs.createWriteStream(target_path); src.pipe(dest); src.on('end', function() { res.render('complete'); }); src.on('error', function(err) { res.render('error'); }); }); 

您在multer的upload.single(<NAME>)函数中使用的upload.single(<NAME>)必须与您在<input type="file" name="<NAME>" ...>

所以你需要改变

var type = upload.single('file')

var type = upload.single('recfile')

在你的app.js

希望这可以帮助。

因为2张图片正在上传! 一个文件扩展名和其他文件没有扩展名。 删除tmp_path(没有扩展名的文件)


src.pipe(dest);

添加下面的代码

fs.unlink(tmp_path); //deleting the tmp_path

这可以用于Api

  const express = require('express'); const bodyParser = require('body-parser'); const app = express(); var multer = require('multer'); const port = 8000; app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.listen(port, ()=>{ console.log('We are live on' + port); }); var upload = multer({dest:'./upload/'}); app.post('/post', upload.single('file'), function(req, res) { console.log(req.file); res.send("file saved on server"); }); 

这也适用于邮递员很好,但文件不附带.jpg扩展任何build议?

Interesting Posts