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

标签 javascript mysql node.js 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;
      }

关于javascript - 在Nestjs中将文件保存在文件系统中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61434084/

相关文章:

javascript - 如何将 async/await 与多个相关集合一起使用

node.js - 在 Heroku 中使用 Docker 部署 Node.js 上的 "Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch"

javascript - 单击表格行上的任意位置,它会检查其在...中的复选框?

javascript - IOS 应用程序 webview SVG ClipPath 问题

php - Doctrine 2 动态查询

MySQL 函数返回要在查询中使用的表名称

php - 计算每个用户 ID 的交易金额

node.js - Node JS 加密密码 aes 256 最大数据大小

javascript - Vue中如何用子路由组件替换父 View

javascript - 如何使用 Javascript/jQuery 创建和 append 图像?