我正在尝试通过带有 request module
的 POST 请求上传照片
根据自述文件,我应该能够做到这一点
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);
}
问题是,这行不通。我收到来自测试服务器的回复,说它转储了 0 个帖子变量。
我已经通过这个小 html 页面确认服务器处于工作状态
<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>
所以问题是,我在请求模块上做错了什么?有没有更好的方法用 Node 发送 multipart/form-data
?
最佳答案
经过一番研究,我决定使用 restler module
.它使分段上传非常容易。
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);
});
});
关于forms - Nodejs POST 请求多部分/表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797670/