search

Home  >  Q&A  >  body text

node.js - node-formidable解析问题?

node代码:

var express = require('express')
var app = express();
var formidable = require('formidable');
app.post('/upload', function(req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        console.log(files);    //打印为空对象
    });
})
app.listen(8888);

前端代码:

<form action="/upload" method="post" enctype="multipart/form-data">
        <input type="file">
        <input type="submit" value="Upload">
    </form>

为什么打印files为空对象?
看了很多例子,几乎试了所有我知道的可能性,不知道哪出错了,求帮助。。。(现在猜测是req对象的问题,不知道是不是因为express框架扩展了req对象,我在原生node里就没问题)

天蓬老师天蓬老师2782 days ago582

reply all(1)I'll reply

  • 迷茫

    迷茫2017-04-17 11:53:45

    This problem took me 5 or 6 hours. . . I tried every possibility.
    In the end, TMD turned out to be this problem:

    Only form elements with their name attribute set can pass their values ​​when submitting the form.

    so, <input type="file" name="upload" multiple="multiple"> is fine.

    reply
    0
  • Cancelreply