angularjs - Unable to write a pdf base64 encoded file into android and ios device using ionic-native/file plugin
问题描述
I am using ionic native ("ionic-native": "2.4.1") and using the FileOpener plugin to open a pdf file which is getting as base64 encoded from the api response.I have tried several options to write the file using File.writeFile() method and it was writing as 0kb.When I use the following method the file was written with some filesize,but it is in invalid format.
import { FileOpener,File } from 'ionic-native';
declare var cordova: any
@Component({
providers: [File,FileOpener]
})
openArrBuffer(b64Data)
{
const base64ToArrayBuffer = (b64Data) => {
const binary_string = window.atob(b64Data);
const len = binary_string.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
File.writeFile(cordova.file.externalDataDirectory,'testnew.pdf',b64Data,{replace: true}).then((response) => {
//console.log('successfully wrote to file3',response);
FileOpener.open( cordova.file.externalDataDirectory+ 'testnew.pdf','application/pdf').then((response) => {
//console.log('opened PDF file successfully3',response);
}).catch((err) => {
//console.log('error in opening pdf file3',err);
});
}).catch((err) => {
//console.log('error writing to file3',err);
});
}
In browser I can able to generate the pdf using the same base64 pdf
const byteCharacters = atob(b64Data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
//console.log(byteArray);
var blobFile = new Blob([byteArray], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(blobFile);
window.open(fileURL, '_blank', 'location=no');
Can anyone suggest a solution to open a base64 pdf in ionic 2
解决方案
推荐阅读
- reference - Prolog比较变量的方法
- firebase - 如何从 Google 表格访问 Cloud Firestore?
- xcode - 观看应用程序 - 在活动方案下看不到模拟器
- reactjs - 我是否正确调用了我的 API?获取 CORS 错误
- cuda - 在共享内存中查找数组的最小索引的问题
- python - Django ImageField 在处理时保持原始图像比例
- amazon-dynamodb - DynamoDB 中的并发更新,是否有任何保证?
- php - 如何对齐不同尺寸的图像
- webpack-encore - 使用 webpack-encore 复制文件
- jackson - 将成功反序列化为 Instant 的默认字符串格式?