I am using Co-Request to read Zip file from http url, and i have below code to read from server..
The code works already. But I dont know how to write the response Zip to a file.
var co = require( "co" );
var request = require( "co-request" );
var options = {
url: ".zip",
headers: {
'Token': Appconfig.Affiliate_Token,
'Affiliate-Id' : Appconfig.Affiliate_Id
}
}
console.log( "Downloading : zip file" );
var j = yield request( options );
Co-Request is actually wrapper for Request and I have found below code to pipe file to stream. But not sure how to write the same using Co-Request with yield.
request.get('.png').pipe(request.put('.png'))
Please help how to write response zip to a file using yield and co-request
I am using Co-Request to read Zip file from http url, and i have below code to read from server..
The code works already. But I dont know how to write the response Zip to a file.
var co = require( "co" );
var request = require( "co-request" );
var options = {
url: "http://www.example./sample.zip",
headers: {
'Token': Appconfig.Affiliate_Token,
'Affiliate-Id' : Appconfig.Affiliate_Id
}
}
console.log( "Downloading : zip file" );
var j = yield request( options );
Co-Request is actually wrapper for Request and I have found below code to pipe file to stream. But not sure how to write the same using Co-Request with yield.
request.get('http://example./img.png').pipe(request.put('http://example./img.png'))
Please help how to write response zip to a file using yield and co-request
Share Improve this question edited Apr 19, 2015 at 13:06 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Apr 12, 2015 at 4:51 amazamaz 4451 gold badge5 silver badges15 bronze badges 2- 2 Check out co-request docs: github./request/request/blob/master/README.md You can pipe response to another stream request('google./… – vromanch Commented Apr 15, 2015 at 13:34
- I don't think you can use generators for piping streams. – Bergi Commented Apr 19, 2015 at 13:36
1 Answer
Reset to default 6 +25I think request cant pipe after data has been emitted from the response
use request instead of co-request, write a promise to achieve this
var co = require('co');
var request = require('request');
var fs = require('fs');
var url = 'http://google./doodle.png';
var requestPipToFile = function(url, filepath) {
return new Promise(function(resolve, reject) {
try {
var stream = fs.createWriteStream(filepath);
stream.on('finish', function() {
console.log("pipe finish");
return resolve(true);
});
return request(url).pipe(stream);
} catch (e) {
return reject(e);
}
});
};
co(function*() {
var value = (yield requestPipToFile(url, './outfile'));
return value;
}).then(function(value) {
return console.log(value);
}).catch(function(err) {
return console.error(err);
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742337969a4425036.html
评论列表(0条)