i'm fetching some binary data over http. My code looks like:
我通过http获取一些二进制数据。我的代码看起来像:
var writeStream = fs.createWriteStream(fileName);
request(url, function(err, res) {
res.socket.pipe(writeStream);
});
now the output file is created but the filesize is 0. The url is correct though, i verified that with wget.
现在已经创建了输出文件,但是filesize是0。url是正确的,我用wget验证了这一点。
Thanks in advance & best regards
谨此致谢。
3 个解决方案
#1
20
The callback for http.request
only supplies one argument, which is a reference to the response of the request. Try
http的回调。请求只提供一个参数,它是对请求响应的引用。试一试
http.request(url, function(res) {
res.pipe(writeStream);
});
Also note that the ClientResponse
implements ReadableStream
, so you should use .pipe
rather than .socket.pipe
.
还要注意,ClientResponse实现了ReadableStream,所以您应该使用.pipe而不是. socket.com pipe。
#2
6
I'm assuming that here request
is from mikeal's request library rather than being an instance of http.request
. In that case you can simply do request(url).pipe(writeStream);
我假设这里的请求来自mikeal的请求库,而不是http.request的实例。在这种情况下,只需执行request(url).pipe(writeStream);
Remember that for debugging purposes, you can always pipe to process.stdout
.
请记住,为了调试目的,您始终可以将管道传输到process.stdout。
#3
3
var writeStream = fs.createReadStream(fileName);
request(url, function(err, res) {
writeStream.pipe(res);
writeStream.on('end', function() {
//res.end({"status":"Completed"});
});
});
#1
20
The callback for http.request
only supplies one argument, which is a reference to the response of the request. Try
http的回调。请求只提供一个参数,它是对请求响应的引用。试一试
http.request(url, function(res) {
res.pipe(writeStream);
});
Also note that the ClientResponse
implements ReadableStream
, so you should use .pipe
rather than .socket.pipe
.
还要注意,ClientResponse实现了ReadableStream,所以您应该使用.pipe而不是. socket.com pipe。
#2
6
I'm assuming that here request
is from mikeal's request library rather than being an instance of http.request
. In that case you can simply do request(url).pipe(writeStream);
我假设这里的请求来自mikeal的请求库,而不是http.request的实例。在这种情况下,只需执行request(url).pipe(writeStream);
Remember that for debugging purposes, you can always pipe to process.stdout
.
请记住,为了调试目的,您始终可以将管道传输到process.stdout。
#3
3
var writeStream = fs.createReadStream(fileName);
request(url, function(err, res) {
writeStream.pipe(res);
writeStream.on('end', function() {
//res.end({"status":"Completed"});
});
});