我正在尝试从 azure blob 获取文件并将该文件发送到 nextjs 前端。然而,我的回复是一个字符串。
这是我的控制器功能
@Get('download/:id')
async getDownloadedFile(@Request() req: JwtRequest, @Param('id') id: string, @Res() res: Response){
const blobName = `/chain/${id}`.toLowerCase();
const container = req.authContext.id.toLowerCase();
const downloadResponse = await this.azureStorageService.getFile(container, blobName)
res.setHeader('Content-Disposition', `attachment; filename="file.txt"`);
res.setHeader('Content-Type', 'application/octet-stream');
downloadResponse.pipe(res);
}
但是在前端,Content-Type 仍然是 application-json 并且响应以字符串形式出现。
您必须确保文件正确地从 Azure Blob 存储流式传输到 NestJS 中的响应,以便将文件从 NestJS 后端发送到 Next.js 前端。
您似乎正在尝试将 downloadResponse 直接通过管道传输到当前代码片段中的响应对象,以便发送文件内容。但您遇到的问题可能与前端处理响应的方式有关,或者与文件流的管理方式有关。
这里有一些解决这个问题的想法:
通过解决这些问题,您应该能够将文件作为二进制数据从 NestJS 传输到 Next.js。如果您仍然遇到问题,请考虑调试每个阶段的数据流以确定问题的潜在根源。