Nodejs POST 请求 multipart/form-data [英] Nodejs POST request multipart/form-data
问题描述
我正在尝试使用 请求模块
根据自述文件,我应该能够做到这一点
var r = request.post("http://posttestserver.com/post.php", requestCallback)var 表单 = r.form()form.append("folder_id", "0");form.append("filename", fs.createReadStream(path.join(__dirname, "image.png")));函数 requestCallback(err, res, body) {控制台日志(正文);}
问题是,这行不通.我收到测试服务器的回复,说它转储了 0 个帖子变量.
我已经通过这个小 html 页面确认服务器处于工作状态
<身体><form action="http://posttestserver.com/post.php?dir=example" method="post" enctype="multipart/form-data">文件:<input type="file" name="submitted"><input type="hidden" name="someParam" value="someValue"/><input type="submit" value="send"></表单></html>
所以问题是,我在请求模块上做错了什么?有没有更好的方法来发送 multipart/form-data
与节点?
经过更多研究,我决定使用 restler 模块
.它使分段上传变得非常容易.
fs.stat("image.jpg", function(err, stats) {restler.post("http://posttestserver.com/post.php", {多部分:真实,数据: {"folder_id": "0","filename": restler.file("image.jpg", null, stats.size, null, "image/jpg")}}).on("完成", 函数(数据) {控制台日志(数据);});});
I am trying to upload a photo via a POST request with the request module
According to the readme I should just be able to do this
var r = request.post("http://posttestserver.com/post.php", requestCallback)
var form = r.form()
form.append("folder_id", "0");
form.append("filename", fs.createReadStream(path.join(__dirname, "image.png")));
function requestCallback(err, res, body) {
console.log(body);
}
The problem is, this doesn't work. I get a reply from the test server saying it dumped 0 post variables.
I have confirmed that the server is in working condition with this little html page
<html>
<body>
<form action="http://posttestserver.com/post.php?dir=example" method="post" enctype="multipart/form-data">
File: <input type="file" name="submitted">
<input type="hidden" name="someParam" value="someValue"/>
<input type="submit" value="send">
</form>
</body>
</html>
So the question is, what am I doing wrong with the request module? Is there a better way to send multipart/form-data
with node?
After some more research, I decided to use the restler module
. It makes the multipart upload really easy.
fs.stat("image.jpg", function(err, stats) {
restler.post("http://posttestserver.com/post.php", {
multipart: true,
data: {
"folder_id": "0",
"filename": restler.file("image.jpg", null, stats.size, null, "image/jpg")
}
}).on("complete", function(data) {
console.log(data);
});
});
这篇关于Nodejs POST 请求 multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!