使用@UploadFile上传文件期间收到的对象类型 [英] Type of object received during file upload using @UploadFile

查看:266
本文介绍了使用@UploadFile上传文件期间收到的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的REST API中,接收的文件对象的类型是什么。

In the REST API below, what is the type of file object that is received.

@Post('/:folderId/documents/:fileName')
@UseInterceptors(FileInterceptor('file'))
@ApiConsumes('multipart/form-data')
@ApiImplicitParam({ name: 'folderId', description: ' Folder Id' })
@ApiImplicitParam({ name: 'fileName', description: ' File Name' })
@ApiImplicitFile({ name: 'file', required: true, description: 'PDF File' })
async uploadFile(@UploadedFile() file, @Param() folderId, @Param() fileName) {
/**
 * I need to know the type of file object (first argument) of uploadFile
 */
    this.folderService.uploadFile(file, folderId, fileName);
}

我需要将请求中收到的文件写入磁盘。怎么做?

I need to write a file received in the request to disk. How to do that?

推荐答案

您可以通过在 MulterOptions中指定目的地路径来保存文件/ code>:

You can save the file by specifying a destination path in the MulterOptions:

// files will be saved in the /uploads folder
@UseInterceptors(FileInterceptor('file', {dest: 'uploads'}))






如果想要更好地控制文件的保存方式,可以改为创建一个multer diskStorage 配置对象:

import { diskStorage } from 'multer';

export const myStorage = diskStorage({
  // Specify where to save the file
  destination: (req, file, cb) => {
    cb(null, 'uploads');
  },
  // Specify the file name
  filename: (req, file, cb) => {
    cb(null, Date.now() + '-' + file.originalname);
  },
});

然后将其传递给存储属性在你的控制器中。

And then pass it to the storage property in your controller.

@UseInterceptors(FileInterceptor('file', {storage: myStorage}))

有关更多配置选项,请参阅 multer docs

For more configuration options, see the multer docs.

这篇关于使用@UploadFile上传文件期间收到的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆