node.js - node-formidable解析问题?
天蓬老师
天蓬老师 2017-04-17 11:51:45
0
1
580

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里就没问题)

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
迷茫

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template