【问题标题】:How to return an image file correctly using nest.js?如何使用nest.js 正确返回图像文件?
【发布时间】:2019-05-26 04:02:28
【问题描述】:

我正在尝试返回请求的图像文件。我的客户正在下载该文件,但我无法显示它,因为它是无效的png 文件。如果我打开存储的文件tmpFile.png,我可以正确看到它。所以问题可能在于我如何将它发送回请求它的客户。

// This is my controller
async getFile(@Param('bucketname') bucketName: string,
            @Param('filename') fileName: string) {
return await this.appService.getFile(bucketName, fileName);


// This is the function called
getFile(bucketName: string, fileName: string) {
    return new Promise(resolve => {
      this.minioClient.getObject(bucketName, fileName, (e, dataStream) => {
        if (e) {
          console.log(e);
        }

        let size = 0;
        const binary = fs.createWriteStream('tmpFile.png');

        dataStream.on('data', chunk => {
          size += chunk.length;
          binary.write(chunk);
        });
        dataStream.on('end', () => {
          binary.end();
          resolve(binary);
        });
      });
    });
  }

【问题讨论】:

标签: javascript rest nestjs


【解决方案1】:

这应该可行:

// This is my controller
async getFile(@Param('bucketname') bucketName: string, @Param('filename') fileName: string, @Res() response) {
  return (await this.appService.getFile(bucketName, fileName)).pipe(response);
}

【讨论】:

    猜你喜欢
    • 2020-02-29
    • 2019-06-02
    • 2021-06-18
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多