Type of object received during file upload using @UploadFile

耗尽温柔 提交于 2019-12-02 17:54:25

问题


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?


回答1:


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'}))

If you want more control over how your file is saved, you can create a multer diskStorage configuration object instead:

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}))

For more configuration options, see the multer docs.



来源:https://stackoverflow.com/questions/53649032/type-of-object-received-during-file-upload-using-uploadfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!