[英]Angular 8, upload (using post multipart/form-data) the result of FileReader.readAsDataURL
My use case:我的用例:
input type="file"
).input type="file"
)。multipart/form-data
.multipart/form-data
。 I have found examples of both uploading ( example 1 , example 2 ) and previewing.我找到了上传(示例 1 、示例 2 )和预览的示例。 There is one example showcasing both .
有 一个示例展示了两者。
My problem is, all of the examples fill the FormData with the File, not the base-encoded URL.我的问题是,所有示例都使用文件填充 FormData,而不是基本编码的 URL。 If previewing and POSTing are separated in time, it seems redundant to store both in the local storage.
如果在时间上预览和发布是分开的,那么将两者都存储在本地存储中似乎是多余的。
So my question is - how to convert the encoded data URL to the format acceptible to FormData.所以我的问题是 - 如何将编码的数据 URL 转换为 FormData 可接受的格式。 Intuitively is should be trivial, since POST should encode the binary probably the same way it does in the URL.
直觉上应该是微不足道的,因为 POST 应该以与在 URL 中相同的方式对二进制文件进行编码。 Is there a standard practice for doing so?
是否有这样做的标准做法?
Additional questions that are relevant:其他相关问题:
Ok, here is what worked for me.好的,这对我有用。 Note the code is copied from other SO posts mentioned in the question.
请注意,代码是从问题中提到的其他 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,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.