i’m using ionic file to save blob which i’m getting in response from an api, response is in form of string which is a blob.
Trying to convert blob(which is in string format) to ArrayBuffer then saving ArrayBuffer to file.writeFile() method.
public saveAsPdfFile(buffer: any, fileName: string): void {
this.getStoragePath().then(url=>{
let successMsg:any;
let data = this.str2ab(buffer);
//this.translate.get('downloadedSuccessfully').subscribe((res: string) => { successMsg = res; });
// atob(this.string_Base64PDF)
this.file.createFile(url, fileName+".pdf", true)
.then(() => {
console.log("file created");
this.file.writeFile(url, fileName+".pdf", data,{ replace: true }).then(() => {
this.showAlert(successMsg);
}).catch((err) => {
console.log(err)
});
})
});
}
str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
file is getting created but when i open error says “filename.pdf is of invalid format” tried same with excel also thows same error. let me know what i’m missing