【问题标题】:How to forward the file stream fetched from axios and return it to the front-end?如何转发从axios获取的文件流返回给前端?
【发布时间】:2021-11-27 09:50:00
【问题描述】:

如何转发从axios获取的文件流返回给前端?

在nest中,我正在使用axios从某处获取内容类型:“application / octet-stream”文件流,我可以获取此文件,但是如何将此文件返回到前端?

【问题讨论】:

    标签: nestjs


    【解决方案1】:
    // service
    this.httpService.get(`...`, {
          params: {
            ...
          },
          responseType: 'arraybuffer'
    });
    
    // controller
    async export(@Req() req: any, @Res() res) {
        res.header('Content-disposition', 'attachment; filename=xxx.xlsx');
        res.header('Content-type', 'application/octet-stream');
    
        const _buffer = await this.Service.export(id);
        return await res.send(_buffer.data);
    }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-06-01
    • 2020-11-12
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2020-12-19
    • 2019-05-23
    • 1970-01-01
    • 2020-10-22
    相关资源
    最近更新 更多