如何使用angular2将base64图像字符串存储在API中



我是angular的新手。

我的任务是转换base64字符串中的图像,并将该字符串存储在API json中。

我已经正确地将图像转换为字符串格式,但我不知道如何将该字符串存储在后端。求你们了,我需要你们。

HTML
----
<div class="col-lg-8">
<input type="file" accept="image/*"
(change)="changeListener($event)">
<div *ngIf="imageToShow">
<img src="{{imageToShow}}" class= "center" alt="">
</div>
</div>
TS
---
imageToShow: string = null;
ngOnInit() {
this.category_info = JSON.parse
('{"category":{"image":"","name":"","description":""
,"definition":"","type":"","title":"","icon_name":""}}');
}
changeListener($event) : void {
this.readThis($event.target);
}
readThis(inputValue: any): void {
var file:File = inputValue.files[0];
var myReader:FileReader = new FileReader();
myReader.onloadend = (e) => {
this.imageToShow = myReader.result;
console.log(this.imageToShow);
}
myReader.readAsDataURL(file);
}  
From the Base64 String, Please remove Content Type string **'data:image/png;base64,'**. You can send the Base64 content string to the Backend API as POST method using Angular HTTPClient Service.
JSON Structure:
{
id: 0,
content-type: :image/png
base64-content : "image base64 content"
}

最新更新