1. 需求:把对象转成二进制文件流上传,就像input上传文件一样,如图:
代码如下:
this.file = {
cells:[
{
id: "e00a94fb-d570-44d4-9f4f-df76aab167c9",
zIndex: 1
}
]
}
const formData = new FormData();
formData.append("id", uuidv4());
formData.append(
"file",
this.file
);
2. 转化:
如何把 this.file
转成如上图所示的二进制流文件呢,使用 Blob
, 完整代码如下:
const formData = new FormData();
formData.append("id", uuidv4());
formData.append(
"file",
new Blob([JSON.stringify(this.file)])
);
api(formData).then((res) => {
console.log("res", res);
});