【发布时间】:2022-08-20 04:46:08
【问题描述】:
我想将pdf、word、excel和ppt uri转换为blob,我使用cordova-plugin-file中的 this.file.resolveLocalFilesystemUrl和this.file.readAsArrayBuffer并获得空缓冲区。所以我的 Blob 也是空的。
这是我的代码
getFiles(){
console.log(\'GetFiles \');
this.filePicker.pickFile().then(uri => {
console.log(\' getFiles uri \'+JSON.stringify(uri));
this.makeFileIntoBlob(uri).then((resolve)=>{
console.log(\' makeFileIntoBlob resolve \'+JSON.stringify(resolve));
}).catch(err=>console.log(\' makeFileIntoBlob Error\', JSON.stringify(err)));
}).catch(err => console.log(\'Error\', err));
}
// FILE STUFF
makeFileIntoBlob(_Path) {
// INSTALL PLUGIN - cordova plugin add cordova-plugin-file
return new Promise((resolve, reject) => {
console.log(\'path \'+_Path);
let fileName = \"\";
this.file.resolveLocalFilesystemUrl(\'file:///\'+_Path).then(fileEntry => {
console.log(\'fileEntry \'+JSON.stringify(fileEntry));
let { name, nativeURL } = fileEntry;
// get the path..
let path = nativeURL.substring(0, nativeURL.lastIndexOf(\"/\"));
console.log(\' file path \'+path);
fileName = name;
console.log(\'file Name \'+fileName);
// we are provided the name, so now read the file into a buffer
return this.file.readAsArrayBuffer(path, name);
}).then(buffer => {
console.log(\'buffer \'+JSON.stringify(buffer));
// get the buffer and make a blob to be saved
let fileBlob = new Blob([buffer], { type: \"application/pdf\" });
console.log(\'fileBlob \'+JSON.stringify(fileBlob));
// pass back blob and the name of the file for saving
// into fire base
resolve({
fileName,
fileBlob
});
}).catch(e => console.log(\'resolveLocalFilesystemUrl e \'+JSON.stringify(e)));
});
}
URI: \"/private/var/mobile/Containers/Data/Application/099A9287-B088-4A92-A052-33CB3577B8F0/tmp/com.focus.messenger-Inbox/ASM9_M02_L02_EN___6a0aa8c4-8cb2-4ba2-b283-f623848d94e0.pdf\"
本机网址:file:///var/mobile/Containers/Data/Application/099A9287-B088-4A92-A052-33CB3577B8F0/tmp/com.focus.messenger-Inbox/ASM9_M02_L02_EN___6a0aa8c4-8cb2-4ba2-b283-f623848d94e0.pdf
路径:file:///var/mobile/Containers/Data/Application/099A9287-B088-4A92-A052-33CB3577B8F0/tmp/com.focus.messenger-Inbox
文件名:file Name ASM9_M02_L02_EN___6a0aa8c4-8cb2-4ba2-b283-f623848d94e0.pdf
缓冲 {}
文件块 {}
标签: angular blob buffer ionic5 cordova-plugin-file