簡體   English   中英

如何使用nest.js正確返回圖像文件?

[英]How to return an image file correctly using nest.js?

我正在嘗試返回請求的圖像文件。 我的客戶正在下載文件,但由於它是無效的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);
        });
      });
    });
  }

這應該工作:

// 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM