POST的NodeJS要求的multipart / form-data的 [英] Nodejs POST request multipart/form-data

查看:1262
本文介绍了POST的NodeJS要求的multipart / form-data的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过与 请求模块

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);
}

的问题是,这是行不通的。我从测试服务器说它倾倒0后变量的答复。

The problem is, this doesn't work. I get a reply from the test server saying it dumped 0 post variables.

我已确认该服务器是在这个小html页面的工作状态

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>

所以,问题是,我在错误的请求模块做什么?有没有更好的方式来发送的multipart / form-data的与节点?

推荐答案

一些研究之后,我决定使用 restler模块 。它使多部分上传真的很容易。

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);
    });
});

这篇关于POST的NodeJS要求的multipart / form-data的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆