NestJs Fastify 配置文件上传

本来项目用的是 Nestjs + Fastify, 因项目需要文件上传功能, 一开始使用的是FileInterceptor 但是会报"TypeError: req.pipe is not a function" 这个错误. 偶然中在 stack overflow 找到了这个问题的解决方法.How to get rid of "TypeError: req.pipe is not a function" nestjs and fastify根据项目修改后的文件上传代码import { I...
摘要由CSDN通过智能技术生成

本来项目用的是 Nestjs + Fastify, 因项目需要文件上传功能, 一开始使用的是 FileInterceptor 但是会报  "TypeError: req.pipe is not a function" 这个错误. 偶然中在 stack overflow 找到了这个问题的解决方法. How to get rid of "TypeError: req.pipe is not a function" nestjs and fastify

根据项目修改后的文件上传代码

import { Injectable } from "@nestjs/common";
import { rootPath } from "src/common/util/root-path";
import CraftEntity from "src/entities/admin/bs-craft.entity";
import { Connection } from "typeorm";
const path= require("path");
const fs = require('fs');

@Injectable()
export class SysFileService {
  constructor(
    private readonly connection: Connection) { }

  async clearFiles() {
    const recordFiles: Set<string> = new Set();

    const all 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NestJS 中,上传多个文件与上传单个文件类似,只需要在控制器中稍作修改即可。 以下是一个上传多个文件的示例: 1. 导入 `MulterModule` 并配置: ```typescript import { Module } from '@nestjs/common'; import { MulterModule } from '@nestjs/platform-express'; @Module({ imports: [ MulterModule.register({ dest: './uploads', }), ], }) export class AppModule {} ``` 2. 在控制器中使用 `@UseInterceptors` 装饰器和 `FilesInterceptor` 拦截器来处理上传的文件: ```typescript import { Controller, Post, UseInterceptors, UploadedFiles } from '@nestjs/common'; import { FilesInterceptor } from '@nestjs/platform-express'; @Controller('upload') export class UploadController { @Post() @UseInterceptors(FilesInterceptor('files')) // 设置上传文件的字段名 async uploadFiles(@UploadedFiles() files) { console.log(files); // 输出上传文件的信息 } } ``` 这里的 `files` 是上传文件的数组,每个元素都包含上传文件的信息。 3. 在客户端发送文件上传请求。与上传单个文件类似,只需要将多个文件添加到 FormData 中即可。 ```typescript import axios from 'axios'; const formData = new FormData(); formData.append('files', file1); formData.append('files', file2); // 将多个文件添加到 FormData 中 axios.post('http://localhost:3000/upload', formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); ``` 以上就是在 NestJS 中上传多个文件的基本步骤。注意,上传多个文件时,需要将多个文件都添加到 FormData 中,并使用相同的字段名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值