我正在尝试使用 Nest 控制器从 GridFS 返回文件。据我所知 Nest 不尊重我的习惯content-type
我设置的标题application/zip
,因为我在返回时收到文本内容类型(参见屏幕截图)。
响应数据图像,错误的内容类型标头 https://i.stack.imgur.com/AmD3p.png
我的巢控制器看起来像这样
@Get(':owner/:name/v/:version/download')
@Header('Content-Type', 'application/zip')
async downloadByVersion(@Param('owner') owner: string, @Param('name') name: string, @Param('version') version: string, @Res() res): Promise<any> {
let bundleData = await this.service.getSwimbundleByVersion(owner, name, version);
let downloadFile = await this.service.downloadSwimbundle(bundleData['meta']['fileData']['_id']);
return res.pipe(downloadFile);
}
这是服务电话
downloadSwimbundle(fileId: string): Promise<GridFSBucketReadStream> {
return this.repository.getFile(fileId)
}
这本质上是对此的传递。
async getFile(fileId: string): Promise<GridFSBucketReadStream> {
const db = await this.dbSource.db;
const bucket = new GridFSBucket(db, { bucketName: this.collectionName });
const downloadStream = bucket.openDownloadStream(new ObjectID(fileId));
return new Promise<GridFSBucketReadStream>(resolve => {
resolve(downloadStream)
});
}
我的最终目标是打电话给download
端点并让浏览器注册它是一个 zip 文件并下载它,而不是在浏览器中查看二进制文件。任何关于需要做什么才能实现这一目标的指导将不胜感激。谢谢阅读