【问题标题】:Not able to upload file using NestJs and Multer无法使用 NestJs 和 Multer 上传文件
【发布时间】:2021-11-09 17:52:48
【问题描述】:

我正在尝试使用 Nestjs 将文件上传到 aws s3 存储桶

  @Post('uploadfile/:bucketName')
  @UseInterceptors(FileInterceptor('file'))
  async addAvatar(@UploadedFile() file: Express.Multer.File, @Param('bucketName') bucketName: string): Promise<S3.ManagedUpload.SendData> {
    return this.s3ManagerService.uploadFile(file.buffer, file.originalname, bucketName);
  }

我的 Postrequest 是:

    let formData = new FormData();

    formData.append("file", fs.createReadStream(path.join(__dirname, `./uploads/${uploadedFileName}.jpg`)), file.data.originalname);

    console.log(JSON.stringify(formData.getHeaders()));

    const response = await axios.post("my_url", formData, {
      ...formData.getHeaders(),
    });

总是出错:

[Nest] 5082 - 14/09/2021, 19:36:48 错误 [ExceptionsHandler] 无法读取未定义的属性“缓冲区” 类型错误:无法读取未定义的属性“缓冲区”

如果我在这里遗漏了什么,你能帮我吗?

【问题讨论】:

    标签: file-upload nestjs multer


    【解决方案1】:

    这说明你的文件没有上传,可能是因为它应该作为multipart/formData发送。试试这个:

        const response = await axios.post("my_url", formData, {
            headers: {
                "content-type": "multipart/form-data"
            },
        });
    

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 2019-12-05
      • 1970-01-01
      • 2016-03-09
      • 2016-10-22
      • 2016-06-02
      • 2021-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多