Skip to content

文件上传

Http-Typedi对上传做了很多包装,并导出了诸多方法(base64二进制分片)等上传方式

TIP

当您使用的时候,只需要导入UploadService工具类并注入到应用程序即可。

二进制上传

现在我们可以在 UploadController 中添加一个简单的(二进制)上传接口。我们使用 Http-Typedi 包提供的 UploadService来实现上传接口。

upload.controller.ts

ts
import { RequestConfig, UploadService, PostMapping } from 'http-typedi'

interface TUploadFile {
  file: File
  id: number
}

@Controller('/upload')
export class UploadController {
  constructor(readonly uploadService: UploadService)
  @PostMapping('file')
  async uploadFile<T = TUploadFile>(configure: RequestConfig<T>) {
    return await this.uploadService.uploadFile<T, Record<string, any>>(
      <RequestConfig<T>>configure
    )
  }
}

以上代码客户端直接调用uploadFile会直接把fileid一并上传至服务端,客户端直接传入选中的 file 对象即可,如有其它参数需要将数据发送至服务端可参考本例进行配置

base64 上传

现在我们可以在 UploadController 中添加一个简单的(base64)上传接口。我们使用 Http-Typedi 包提供的 UploadService来实现上传接口。

upload.controller.ts

ts
import { RequestConfig, UploadService, PostMapping } from 'http-typedi'

interface TUploadFile {
  file: File
  id: number
}

@Controller('/upload')
export class UploadController {
  constructor(readonly uploadService: UploadService)
  @PostMapping('file')
  async uploadFile<T = TUploadFile>(configure: RequestConfig<T>) {
    return await this.uploadService.uploadBase64<T, Record<string, any>>(
      <RequestConfig<T>>configure
    )
  }
}

以上代码客户端直接调用uploadBase64会直接把fileid进行处理一并上传至服务端, 客户端直接传入选中的 file 对象即可,如有其它参数需要将数据发送至服务端可参考本例进行配置