Multer文件上传错误错误:意外字段

时间:2016-11-08 11:20:34

标签: node.js file upload multer

我正在尝试使用multer在NodeJS上传文件,但它会出现Error: Unexpected field错误。

我正在关注本教程http://code.runnable.com/VNX-T8fdt5Y4X-mv/fileupload-for-node-js-and-hello-world

这是我的代码:

var express=require("express");
var multer  = require('multer');
var app=express();
var done=false;

/*Configure the multer.*/

//app.use();

/*Handling routes.*/

app.get('/',function(req,res){
    res.sendfile("./index.html");
});

app.post('/',multer({ dest: './uploads/'}).single('upl'),function(req,res){
    if(done==true){
        console.log(req.files);
        res.status(204).end();
    }
});

/*Run the server.*/
app.listen(3000,function(){
    console.log("Working on port 3000");
});

在HTML

                                                     

我面临的错误是

Error: Unexpected field
at makeError (C:\Users\Abhishek\Desktop\OSPL_Energy\Node JS\node_modules\multer\lib\make-error.js:12:13)
at wrappedFileFilter (C:\Users\Abhishek\Desktop\OSPL_Energy\Node JS\node_modules\multer\index.js:39:19)
at Busboy.<anonymous> (C:\Users\Abhishek\Desktop\OSPL_Energy\Node JS\node_modules\multer\lib\make-middleware.js:112:7)
at emitMany (events.js:127:13)
at Busboy.emit (events.js:201:7)
at Busboy.emit (C:\Users\Abhishek\Desktop\OSPL_Energy\Node JS\node_modules\busboy\lib\main.js:31:35)
at PartStream.<anonymous> (C:\Users\Abhishek\Desktop\OSPL_Energy\Node JS\node_modules\busboy\lib\types\multipart.js:213:13)
at emitOne (events.js:96:13)
at PartStream.emit (events.js:188:7)
at HeaderParser.<anonymous> (C:\Users\Abhishek\Desktop\OSPL_Energy\Node JS\node_modules\dicer\lib\Dicer.js:51:16)

1 个答案:

答案 0 :(得分:2)

HTML文件名和myimage应与.single相同(&#39; upl&#39;)

所以代码是

var express=require("express");
var multer  = require('multer');
var app=express();
var done=false;

/*Configure the multer.*/

//app.use();

/*Handling routes.*/

app.get('/',function(req,res){
    res.sendfile("./index.html");
});

app.post('/',multer({ dest: './uploads/'}).single('myimage'),function(req,res){
    if(done==true){
        console.log(req.files);
        res.status(204).end();
    }
});

/*Run the server.*/
app.listen(3000,function(){
    console.log("Working on port 3000");
});