在Angular中上传文件时请求内容类型吗?

问题描述 投票:0回答:1

我有一个带有文件输入的表格。求和时,我将其上传到我的API,如下所示:

const formData = new FormData();
formData.append('file', this.fileToUpload, this.fileToUpload.name);
this.uploadService.uploadFile(formData).subscribe(res => this.res = res);

请求内容类型为multipart/form-data

是否可以上传具有application/json内容类型的文件以直接将其解析到API端的DTO?

angular angular-forms angular-file-upload
1个回答
0
投票

要设置内容类型,您需要传递类似文件的对象

 const json = JSON.stringify(yourObject);
    const blob = new Blob([json], {
      type: 'application/json'
    });
    const formData = new FormData();
    formData.append("document", blob);
   this.uploadService.uploadFile(formData).subscribe(res => this.res = res);
© www.soinside.com 2019 - 2024. All rights reserved.