繁体   English   中英

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

[英]Saving files in file system in Nestjs

我想将使用multipart/form-data从客户端发送的图像的URL存储在我的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?

  }
}

尝试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM