我有一个带有文件输入的表格。求和时,我将其上传到我的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?
要设置内容类型,您需要传递类似文件的对象
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);