简体   繁体   中英

Saving files in file system in Nestjs

I want to store the URL of images sent from client using multipart/form-data in my MySQL database. I followed the docs but I can't really find out how I should save an image in server file system and return the URL back to the client.

This is my code:

@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?

  }
}

try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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