请教 request上传文件 已上传字节数怎么获取
再https://github.com/request/request上只看到了 request .on(‘data’, function(chunk){ bytes += chunk } 这个方法获取接收到的字节
但是没找到获取已经上传的字节数的方法
3 回复
在要上传的流和request之间加一层双向流
比如:
const through2 = reuqire('through2');
fs.createReadStream('./somefile').pipe(through2(function (chunk, encoding, callback) {
console.log(chunk.length);
this.push(chunk);
callback();
})).pipe(request('http://somehost/'))
如果你用的是表单上传,就把上面的fs可读流,换成request中用到的form-data库的实例
@soda-wy 非常感谢百忙中帮我解答,我顺着你给我的提示我终于解决了 我在贴一下我这边的代码方便以后需要的人吧。
client code cli.png
server code QQ截图20171225115150.png