当尝试将 formData 转换为 express js 时,服务器无法识别 multipart/form
当尝试将 formData 转换为 express js 时,服务器无法识别 multipart/form
我正在尝试将表单数据上传到快速服务器。
在我的 express js 服务器上,我有以下内容:
router.post( /uploads/:id , function(req, res) {
res.send(req.body);
当尝试将 formData 转换为 express js 时,服务器无法识别 multipart/form
我正在尝试将表单数据上传到快速服务器。 在我的 express js 服务器上,我有以下内容:
router.post( /uploads/:id , function(req, res) {
res.send(req.body);
ct title = req.;
ct file = req.file;
});
我发回 req.body 以查看它是否正确命中了 url。 当我使用以下内容时:
ct respe = await fetch( , {
method: POST ,
headers: {
Content-Type : application/json
},
body: JSO.stringify(form) });
ct respeData = await respe.json();
cole.log(respeData);
我得到转换为 json 的 FormData 的响应。 但是当我这样运行时:
ct file = (`${desktopDir}/ai2html-output/home-pro-Artboard_1.jpg`);
ct title = My file fsf ;
ct form = new FormData();
form.append( title , title);
form.append( file , file);
ct respe = await fetch( , {
method: POST ,
headers: {
Content-Type : multipart/form-data
},
body: form });
ct respeData = await respe.json();
然后返回的是一个空对象: {}
我必须在我的快递服务器上设置一些东西来接收表单数据吗?我需要某种类型的中间件吗?
这也是我来自服务器的请求标头响应:
:method: POST
:path: /uploads/124
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
content-length: 17
content-type: multipart/form-data
origin: file://
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: cross-site
回答如下:
要在您的
Express服务器中处理
multipart/form-data
,您必须配置一个中间件。例如
multer
图书馆。
演示:
Server.js
ct express = require(express);
app = express();
ct multer = require(multer);
ct upload = multer({ dest: uploads/ });
//----------------------vvvvvvvvvvvvvvvvvvvvvv <------- middleware
app.post(/uploads/:id, upload.single(file), function (req, res) {
ct title = req.;
cole.log(req.file);
/*
Output of req.file:
{
fieldname: file ,
originalname: test.jpg ,
encoding: 7bit ,
mimetype: image/jpeg ,
destination: uploads/ ,
filename: 61d00f21b981bdcb26082b41b58eb ,
path: uploads\\61d00f21b981bdcb26082b41b58eb ,
size: 0
}
*/
res.json({ msg: `file ${title} uploaded successfully!` });
});
app.listen(124, () => {
cole.log(app is running on 124);
});
上传.js
var fs = require(fs);
ct fetch = require(node-fetch);
var FormData = require(form-data);
ct form = new FormData();
ct title = Test;
ct file = (`./test.jpg`);
form.append(title, title);
form.append(file, file);
(async () => {
try {
ct respe = await fetch(http://localhost:124/uploads/124, {
method: POST,
// headers: {
// Content-Type: multipart/form-data,
// },
body: form,
});
ct respeData = await respe.json();
cole.log(respeData); // you got { msg: file Test uploaded successfully! }
} catch (error) {
(error);
}
})();
注意: Fetch 会自动设置标题,所以只需带走标题
Content-Type: multipart/form-data
.
如果要将上传的文件保存到特定文件夹“uploads/”,其标题带有req.,将此配置添加到multer:
ct path = require(path);
ct storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, uploads/);
},
filename: (req, file, cb) => {
ct title = req.;
cb(null, title + ());
},
});
ct upload = multer({ storage: storage });
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2024-06-03 21:56:30
推荐阅读
留言与评论(共有 16 条评论) |
本站网友 高岛屋 | 10分钟前 发表 |
multipart/form-data } | |
本站网友 郭大宝 | 5分钟前 发表 |
path | |
本站网友 madewell | 21分钟前 发表 |
form | |
本站网友 狼疮肾 | 7分钟前 发表 |
headers | |
本站网友 电焊 | 23分钟前 发表 |
path | |
本站网友 沈慧芬 | 12分钟前 发表 |
}); ct respeData = await respe.json(); cole.log(respeData); // you got { msg | |
本站网友 自动挡 | 24分钟前 发表 |
// } | |
本站网友 王老吉官方网站 | 26分钟前 发表 |
multipart/form-data. 如果要将上传的文件保存到特定文件夹“uploads/” | |
本站网友 蟹黄汤包 | 1分钟前 发表 |
我有以下内容: router.post( /uploads/ | |
本站网友 银黄颗粒 | 1分钟前 发表 |
JSO.stringify(form) }); ct respeData = await respe.json(); cole.log(respeData); 我得到转换为 json 的 FormData 的响应 | |
本站网友 营口纽约纽约 | 17分钟前 发表 |
当我使用以下内容时: ct respe = await fetch( | |
本站网友 寿命最长的鸟 | 17分钟前 发表 |
file Test uploaded successfully! } } catch (error) { (error); } })(); 注意: Fetch 会自动设置标题 | |
本站网友 科文中心电影院 | 5分钟前 发表 |
multipart/form-data origin | |
本站网友 texturepacker | 13分钟前 发表 |
path | |
本站网友 苏合香丸 | 2分钟前 发表 |
(req |