简体   繁体   English

Angular 8,上传(使用post multipart/form-data)FileReader.readAsDataURL的结果

[英]Angular 8, upload (using post multipart/form-data) the result of FileReader.readAsDataURL

My use case:我的用例:

  1. A user opens an image file (using input type="file" ).用户打开一个图像文件(使用input type="file" )。
  2. The image is then displayed on screen, and stored in the local storage然后将图像显示在屏幕上,并存储在本地存储中
  3. At some later point (days) the user clicks a button, and the image should be uploaded to the server, via API based on a POST using multipart/form-data .在稍后的某个时间点(几天),用户单击一个按钮,图像应该通过基于 POST 的 API 上传到服务器,使用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:其他相关问题:

  1. how to upload image in base64 on server 如何在服务器上以base64上传图像
  2. Converting base64 to blob in javascript在javascript中将base64转换为blob

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.

相关问题 Angular 7 Post 文件内容作为 multipart/form-data - Angular 7 Post file contents as multipart/form-data Angular HttpClient发布两个文件(Multipart / form-data) - Angular HttpClient Post Two Files (Multipart/form-data) 无法 POST 请求多部分/表单数据角度 - Cannot POST request multipart/form-data angular 如何将多部分/表单数据从 Angular 发布到 Nodejs Multer? - How to post multipart/form-data from Angular to Nodejs Multer? 将多部分/表单数据发布到端点 - Post multipart/form-data to endpoint 使用ngx-mat-file-input从Angular上载文件作为多部分/表单数据 - Upload File as multipart/form-data from Angular with ngx-mat-file-input 如何将文件从Angular 2上传到Django服务器? (enctype =“ multipart / form-data”) - How to upload a file from Angular 2 to Django Server? (enctype=“multipart/form-data”) angular10 应用程序中多部分/表单数据的图像上传 API 错误 - Image upload API error for multipart/form-data in my angular10 application 如何从角度发布多部分/表单数据到Django REST API - How to post multipart/form-data from angular to Django REST API Angular 5-如何将null设置为HTTP POST multipart / form-data的内容类型 - Angular 5 - How to set null as content type for HTTP POST multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM