我正在尝试使用我的 Nest 控制器从 GridFS 返回一个文件。据我所知,nest 不尊重我content-type设置的自定义标头application/zip,因为我在返回时收到了文本内容类型(请参见屏幕截图)。
我的巢控制器看起来像这样
@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);
}
Run Code Online (Sandbox Code Playgroud)
这是服务电话
downloadSwimbundle(fileId: string): Promise<GridFSBucketReadStream> {
return this.repository.getFile(fileId)
}
Run Code Online (Sandbox Code Playgroud)
这本质上是对此的传递。
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 …Run Code Online (Sandbox Code Playgroud)