我的用例:
input type="file"
)。 multipart/form-data
通过API上传到服务器。我已经找到了上载(example 1,example 2)和预览的示例。有one example showcasing both。
我的问题是,所有示例都使用File而不是基本编码的URL填充FormData。如果预览和发布在时间上分开,则将两者都存储在本地存储中似乎是多余的。
所以我的问题是-如何将编码的数据URL转换为FormData可接受的格式。从直觉上讲应该是微不足道的,因为POST应该以与在URL中相同的方式对二进制文件进行编码。有没有这样做的标准做法?
其他相关问题:
好,这对我有用。请注意,该代码是从问题中提到的其他SO帖子中复制的。
private b64toArrayBuffer(dataURI) {
const byteString = atob(dataURI.split(',')[1]);
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return ia;
}
private b64toBlob(dataURI, mimetype) {
return new Blob([this.b64toArrayBuffer(dataURI)], {
type: mimetype
});
}
const blob = this.b64toBlob(media.url, media.mimetype);
formData.append('file', blob);
return this.http.post(uploadUrl, formData,