我正在尝试使用Nest控制器从GridFS返回文件。据我所知,Nest没有遵守我设置为application/zip
的自定义content-type
头部,因为我在返回时收到了文本内容类型(请参见屏幕截图)。
我的Nest控制器如下所示:
@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)
});
}
我的最终目标是调用
下载
端点,并使浏览器注册它为zip文件并下载它,而不是在浏览器中看到二进制文件。如果您能指导我如何实现这一目标,我将非常感激。谢谢阅读。