【发布时间】:2021-03-23 22:54:25
【问题描述】:
我需要向服务器发送一个多部分请求,其中包含一个文件 (document_example.pdf) 和一个包含 Angular 7 中的数据对象 (data_object_example.json) 的 json 文件。
data_object_example.json -> { “属性1”:“名称”, “属性 2”:“第二个名称” }
我知道如何创建多部分请求,但我不知道如何通过对象创建 json 文件。
谢谢。 ;)
回答:感谢HaiTH
const docJson = {
fileDocType: file.name.split('?')[0].split('.').pop(),
fileName: file.name
}
const fileJson = new File([JSON.stringify(docJson)], "file.json", {type: "application/json'"});
const formData = new FormData();
formData.append('json', fileJson);
【问题讨论】:
-
为什么不只是 JSON.stringify() 数据对象并将其作为多部分请求中的另一个字段传递?
-
您好,因为我需要将文件类型 json 传递给服务器...而且我无法更改后端。
标签: javascript html json angular typescript