[英]How to return an image file correctly using nest.js?
I'm trying to return the requested image file. 我正在尝试返回请求的图像文件。 My client is downloading the file, but I can't display it because it's an invalid png
file. 我的客户正在下载文件,但由于它是无效的png
文件,所以无法显示。 If I open the stored file tmpFile.png
, I can see it correctly. 如果打开存储的文件tmpFile.png
,则可以正确看到它。 So probably the problem is on how I'm sending it back to the client asking for it. 因此,问题可能出在我如何将其发送回客户那里。
// 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 should works: 这应该工作:
// 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.