在NodeJS中使用Multer的意外字段错误

时间:2016-06-17 23:35:23

标签: node.js file-upload multer body-parser

错误:

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 express = require('express');
var http = require('http');
var path = require('path');
var fs = require('fs');
var https = require("https");
var multer = require('multer');

process.env.PATH = '/usr/bin';
// all environments

app.set('port', process.env.PORT || 443); //Environment or HTTPS only
//app.set('view engine', 'html');

//HTTP
var redirectApp = express()
  , redirectServer = http.createServer(redirectApp);

redirectApp.use(function (req, res, next) {
  if (!req.secure) {
    return res.redirect('https://' + req.headers.host + req.url);
  }
  next();
});

redirectServer.listen(80, function() {
  console.log("Redirect instance running on port 80.");
});

__dirname = '/var/www/html';

app.all('*', function(req, res, next) {  
  res.header('Access-Control-Allow-Origin', "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(bodyParser.json({limit: "50mb"}));
app.use(useragent.express());

app.set('views', path.join(__dirname));

app.use(morgan('combined'));
app.use(express.static('/var/www/html'));

app.engine('html', require('ejs').renderFile);

app.post("/postFile", multer().array(), function (req, res, next) {
  console.log("please work");
});

require('./routes/main.js')(app);

https.createServer(options, app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

我在前端使用KendoUI文件上传:

                        <form id="forFile" action="/postFile" method="POST" enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="inputFile">Upload Attachments</label>
                                <div class="col-sm-offset-2 col-sm-10 pull-right">
                                    <input type="file" id="inputFile" name="inputFile">
                                </div>
                            </div>
                        </form>

初始化为:

$("#inputFile").kendoUpload({
        multiple: false,
        async: {
            saveUrl: "postFile",
            removeUrl: "removeFile",
            autoUpload : true
        },
        complete: onComplete
    });

我知道这是关于堆栈溢出的几个问题的近似重复,但所有这些问题的答案是使name属性与upload.array或upload.single参数匹配(我已尝试过两者),其中目前我的代码已满足,您可以通过抓取我上传的路线看到:

var multer = require("multer");
var upload = multer({ dest: "upload/" });
var type = upload.array('inputFile', 1); //KendoUI

app.post('/postFile', type, function (req, res) {


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'); });


  sendJsonResponse(res, 200, "you posted a file");
});

0 个答案:

没有答案