在Nestjs中将文件保存在文件系统中

mah*_*hdi 9 javascript mysql node.js nestjs

我想将URL客户端发送的图像存储multipart/form-data在我的MySQL数据库中。我遵循了文档,但我无法真正找出应该如何将图像保存在服务器文件系统中并将 URL 返回给客户端。

这是我的代码:

@ApiTags('product-image')
@Controller('product-image')
export class ProductImageController {
  constructor(public service: ProductImageService) {
  }

  @Post('upload')
  @UseInterceptors(FilesInterceptor('files'))
  uploadImage(@UploadedFiles() files) {
    console.log(files);
    // How can I save image files in
    // a custom path and return the path
    // back to the client?

  }
}
Run Code Online (Sandbox Code Playgroud)

Kal*_*dhi 12

尝试这个

import { FilesInterceptor } from '@nestjs/platform-express';
import { diskStorage } from 'multer';

@Post('upload')
    @UseInterceptors(
        FilesInterceptor('files', 20, {
          storage: diskStorage({
            destination: './uploads/',
            filename: editFileName,
          }),
        //   fileFilter: imageFileFilter,
        }),
      )
      uploadMultipleFiles(@UploadedFiles() files) {
        const response = [];
        files.forEach(file => {
          const fileReponse = {
            filename: file.filename,
          };
          response.push(fileReponse);
        });
        return response;
      }
Run Code Online (Sandbox Code Playgroud)