gpt4 book ai didi

node.js - 使用 connect-multiparty 上传的文件返回空对象

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:50 25 4
gpt4 key购买 nike

尝试使用 connect-multiparty 获取文件详细信息,但它返回一个空对象。

var express = require('express'),
bodyParser = require('body-parser'),
app = express();
var multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty();
app.use( multipartyMiddleware );


app.post('/testupload1', function(req, res){
var body = req.body; var file = req.files;
console.log(file) // or console.log(body) both empty.
}

表格:

<form action="testupload1" method="post" enctype="multipart/form-data">

<input type="file" name="file"> <br />
<input type="submit" value="Upload">

当我上传 jpeg 文件时,在控制台中获取一个空对象 {}。我究竟做错了什么?请帮忙。

最佳答案

我想这可能对你有帮助。

var express = require('express'),
bodyParser = require('body-parser'),
app = express();
var multiparty = require('connect-multiparty'),
multipartyMiddleware = multiparty();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));


app.post('/testupload1', multipartyMiddleware, function(req, res){
var file = req.files;
console.log(file)
});


app.get('/file', function (req, res, next) {
res.end('<!DOCTYPE html>'
+ '<html lang="en">'
+ '<head>'
+ '<meta charset="UTF-8">'
+ '<title>Title</title>'
+ '</head>'
+ '<body>'
+ '<form action="testupload1" method="post" enctype="multipart/form-data">'
+ '<input type="file" name="file"> <br />'
+ '<input type="submit" value="Upload">'
+ '</form>'
+ '</body>'
+ '</html>')
});

app.listen(3008);

enter image description here

关于node.js - 使用 connect-multiparty 上传的文件返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com