【发布时间】:2021-02-03 09:14:41
【问题描述】:
我有一个要求,我需要将所选图像(表单组的一部分)转换为字节数组,该字节数组需要作为发布请求的一部分发送到后端。
HTML 组件:
<div class="form-group">
<label>Image</label>
<input type="file" accept="image/*" class="form-control" formControlName="productImage" onchange="displaySelectedImageFunc(this);">
<img *ngIf="displaySelectedImage" src="{{selectedImageUrl}}">
<span class="error-message" *ngIf="itemRegistrationForm.controls.productImage.dirty && itemRegistrationForm.controls.productImage.errors">Select Image of the product</span>
</div>
【问题讨论】:
-
你可以试试这个吗? stackoverflow.com/questions/53390580/…
-
@Bhavin 嗨,我需要做相反的事情,即将图像转换为字节数组
-
他们使用了“btoa”,你可以尝试将你的base 64字符串传递给“atob”参考:stackoverflow.com/questions/28185781/…
-
我找到了一个很有用的 npm 包npmjs.com/package/image-to-base64。从 base64 到字节数组的转换应该更容易stackoverflow.com/questions/42360999/…
-
你可以使用 FileReader.readAsArrayBuffer()
标签: angular typescript angular-forms