小编fer*_*can的帖子

NestJS 从 GridFS 返回一个文件

我正在尝试使用我的 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)

zip get httpresponse http-headers nestjs

4
推荐指数
1
解决办法
4270
查看次数

标签 统计

get ×1

http-headers ×1

httpresponse ×1

nestjs ×1

zip ×1