如何读取文件并发送到其他URL

时间:2012-03-21 12:00:20

标签: node.js filestream

如何从文件系统中读取文件并将其发送到另一个URL?它类似于我们在其他语言中使用的CURL请求。

我试过以下,但这不起作用。

fs.createReadStream(files.upload.path).pipe(request.post('http://localhost/test.php', function (err, response, body) {
                if (err) {
                    throw err;
                } else {
                    console.log(body);
                    res.end();
                }
            }));

1 个答案:

答案 0 :(得分:1)

你的论点中有错误。您希望将回调发送到request.post,而不是pipe

fs.createReadStream(files.upload.path).pipe(request.post('http://localhost/test.php', function (err, response, body) {
                if (err) {
                    throw err;
                } else {
                    console.log(body);
                    res.end();
                }
            }));

此外,异步代码中的throw错误通常是个坏主意。