Save image

This commit is contained in:
Willem Dantuma
2021-03-01 22:03:18 +01:00
parent 47f3238edd
commit 2f1c5210ea
7 changed files with 86 additions and 15 deletions

View File

@@ -0,0 +1,44 @@
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { IUser } from '../models/user';
import { HttpClient,HttpHeaders } from "@angular/common/http";
import { AppConfig } from "../shared/app.config";
@Injectable({
providedIn: 'root',
})
export class ImageService {
constructor(public httpClient: HttpClient, public appConfig: AppConfig) {
}
ApiEndpoint() {
return this.appConfig.getConfig("apiEndPoint");
}
putImage(endpoint:string,blob:Blob) {
const formData = new FormData();
formData.append('file', blob,blob.type);
return this.httpClient.put<any>(endpoint,formData);
}
b64toBlob(b64Data:string, contentType?:string):Blob {
const sliceSize = 512;
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: contentType});
return blob;
}
}