Некоторое время назад я создал это
var base64;
var dataTypeWithB64 = 'data:image/jpeg;base64,' + base64;
public writeFile(dataTypeWithB64: any, base64:any,) {
var fileName ="file Name";
var rootPath = "app path"
var contentType = this.getContentType(dataTypeWithB64);
// var content
var DataBlob = this.base64toBlob(base64, contentType);
// here iam mentioned this line this.file.externalRootDirectory is a native pre-defined file path storage. You can change a file path whatever pre-defined method.
var filePath = this.file.externalRootDirectory + rootPath;
console.log(filePath , " 1");
console.log(fileName , " 2");
console.log(DataBlob , " 3");
console.log(contentType , " 4");
this.file.writeFile(filePath, fileName, DataBlob, contentType).then((success) => {
console.log("File Writed Successfully", success);
}).catch((err) => {
console.log("Error Occured While Writing File", err);
})
}
//here is the method is used to get content type of an bas64 data
public getContentType(base64Data: any) {
let block = base64Data.split(";");
let contentType = block[0].split(":")[1];
return contentType;
}
//here is the method is used to convert base64 data to blob data
base64toBlob(b64Data, contentType) {
contentType = contentType || '';
let sliceSize = 512;
let byteCharacters = atob(b64Data);
let byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
let slice = byteCharacters.slice(offset, offset + sliceSize);
let byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
let blob = new Blob(byteArrays, {
type: contentType
});
// console.log(blob)
return blob;
}