使用流下载大文件时的 Axios 事件 [英] Axios events when downloading large files with streams

查看:33
本文介绍了使用流下载大文件时的 Axios 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器端使用 axios.

I'm using axios on server side.

我想下载文件..从技术上讲,这应该与字节范围一起使用

I want to download big files .. technically this should be used with byte-ranges

  1. axios 是否处理字节范围请求,以便仅在所有响应准备就绪时调用回调函数
  2. 如果 1 不正确,我应该自己处理数据块吗?
  1. Does axios handle the byte-range request so that the callback function is only called when all the response is ready
  2. If 1 is not true, should I handle data chunks myself ?

在下面的代码中:

axios({
  url: params.url,
  method: 'GET',
  responseType: 'stream' // important
}).then(function (response) {

  logger.debug('__download() : done!')
  let contentType = response.headers['content-type']
  let contentLength = response.headers['content-length']
  var writer = new streams.WritableStream()

  response.data.pipe(writer)
  // ....
})

我应该等待诸如 response.on('end') 之类的东西吗?

Am I supposed to wait for something like response.on('end')?

我这样做的目的是获取缓冲区的大小(我可以通过 writer.getBuffer() 获取)

The purpose of what I'm doing is to get the size of the buffer (which I could get by writer.getBuffer())

感谢您的任何提示!

推荐答案

我发现要下载流,在内存中在我的情况下,我不得不等待我的 writer (writer.on('finished',cb) vs response.on('end', cb))(不确定是否有类似 response.on('end'))...

I found out that to download the stream, in memory in my case, I had to wait for the event on my writer (writer.on('finished',cb) vs response.on('end', cb )) (not sure if there is something like response.on('end'))...

var stream = require('stream');
var util = require('util');
var Writable = stream.Writable;


function MemoryStream(options) {
    if (!(this instanceof MemoryStream)) {
        return new MemoryStream(options);
    }
    Writable.call(this, options); // init super
}
util.inherits(MemoryStream, Writable);

MemoryStream.prototype._write = function (chunk, enc, cb) {
    var buffer = (Buffer.isBuffer(chunk)) ?
        chunk :
        Buffer.alloc(chunk, enc);

    if (Buffer.isBuffer(this.memStore)) {
        this.memStore = Buffer.concat([this.memStore, buffer]);
    } else {
        this.memStore = buffer
    }
    cb();
};


MemoryStream.prototype.toBuffer = function () {
    return this.memStore
};




module.exports = MemoryStream

然后在我的下载功能中:

and then in my download function :

axios({
  url: params.url,
  method: 'GET',
  responseType: 'stream' // important
}).then(function (response) {

  logger.debug('__download() : done!')
  let contentType = response.headers['content-type']
  let contentLength = response.headers['content-length']
  var writer = new MemoryStream()

  response.data.pipe(writer)

    writer.on('finish', function () {

        var b = writer.toBuffer()

        let computedContentLength = b.byteLength

        if (!contentLength) { contentLength = computedContentLength }

        return callback(null, { 'foo':'bar'})

    });
})

这篇关于使用流下载大文件时的 Axios 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆