表达文件上传返回400错误

时间:2013-11-03 04:24:23

标签: node.js express nodejitsu

我有一个需要处理文件上传的快速应用程序,但由于某种原因它无法正常工作。我收到一个神秘的错误信息(nodejitsu / local),我不知道是什么问题。

这是我的路线:

var routes = require('../routes');

module.exports = function(app) {
  app.get('/', routes.index);
  app.get('/import', routes.import.form);
  app.post('/import/upload', routes.import.submit);
};

这是我的功能:

exports.import = {
    form: function(req, res) {
        renderTemplate(res, 'form');
    },
    submit: function (req, res) {
        res.send(console.dir(req.files));
    }
}

这是我的HTML:

<form role="form" action="/import/upload" method="post" enctype="multipart/form-data">
  <div class="form-group">
    <label for="exampleInputFile">Upload</label>
    <input type="file" id="csvFile">
    <p class="help-block">Please select .csv file.</p>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

以下是错误消息:

Error: Expected CR Received 45
at Form._write (/opt/run/snapshot/package/node_modules/express/node_modules/connect/node_modules/multiparty/index.js:187:49)
at doWrite (/opt/run/snapshot/package/node_modules/express/node_modules/connect/node_modules/multiparty/node_modules/readable-stream/lib/_stream_writable.js:262:12)
at writeOrBuffer (/opt/run/snapshot/package/node_modules/express/node_modules/connect/node_modules/multiparty/node_modules/readable-stream/lib/_stream_writable.js:249:5)
at Form.Writable.write (/opt/run/snapshot/package/node_modules/express/node_modules/connect/node_modules/multiparty/node_modules/readable-stream/lib/_stream_writable.js:196:11)
at write (_stream_readable.js:583:24)
at flow (_stream_readable.js:592:7)
at _stream_readable.js:560:7
at process._tickCallback (node.js:415:13)

1 个答案:

答案 0 :(得分:11)

您的input[type=file]没有name属性,这会扰乱Express / Connect。

所以试试这个:

<input type="file" id="csvFile" name="csvFile">