当我请求远程图片的时候用createWriteStream单个请求没问题,,请求多了,就会出现延迟加载,,谁有更好的方法啊?贴上我的代码.我用的是ruby coffeescript的语法.
request = require 'superagent'
request
.get('mp.weixin.qq.com/cgi-bin/getheadimg?fakeid=' + options.takeid)
.set('Cookie', cookie)
.end (res) ->
filename = __basename + '/public/avatar/'+options.takeid+'.jpg'
f = fs.createWriteStream(filename,
flags: "w"
encoding: "binary"
)
res.on "data", (data) ->
f.write data
options.res.write data
res.on "end", (data) ->
f.end()
options.res.end()
1 回复
可不可以不用fs,直接显示?
var getHeadImage = function (takeid, cookie) {
return function (request, response, next) {
var url = 'mp.weixin.qq.com/cgi-bin/getheadimg?fakeid=' + takeid;
var output = function (res) {
var image = '';
res.setEncoding('binary');
res.on('data', function (chunk) {
image += res.statusCode == 200 ? chunk : "";
});
res.on('end', function () {
response.writeHead(200, {
'Content-type': res.headers['content-type']
});
response.write(image, 'binary');
response.end();
});
};
superagent.get(url).set('Cookie', cookie).end(output);
}
};