Ben*_* D. 0 ionic-framework capacitor
我正在使用 Angular 9 / Ionic 5 / Capacitor 2.0 开发混合应用程序。我正在尝试将来自 API 的文件(基本上是 PDF 或图像文件)作为 Blob 保存到我的移动设备(IOS/Android)的文件系统中。我的最终目标是重现文件的类似浏览器的下载功能(允许用户将其保留在手机上)。
这是我的代码:
downloadNativeFile(blob: Blob, fileName: string){
let reader = this.getFileReader();
reader.onloadend = (readerEvent) => {
if(reader.error){
console.log(reader.error);
} else {
let base64data: any = readerEvent.target['result'];
try {
Filesystem.writeFile({
path: fileName,
data: base64data,
directory: FilesystemDirectory.Data
}).then((res)=>{
Filesystem.getUri({
directory: FilesystemDirectory.Data,
path: fileName
}).then((getUriResult) => {
const path = getUriResult.uri;
console.log("The file's path : " + path);
console.log(Capacitor.convertFileSrc(getUriResult.uri));
}, (error) => {
console.log(error);
});
}).catch((err)=>{
console.log("Error", err);
});
} catch(e) {
console.error('Unable to write file', e);
}
}
}
reader.readAsDataURL(blob);
}
Run Code Online (Sandbox Code Playgroud)
执行此代码我没有收到任何错误,我的控制台打印保存文件的路径:
The file's path : /DATA/sample.pdf
http://localhost/_capacitor_file_/DATA/sample.pdf
Run Code Online (Sandbox Code Playgroud)
尽管如此,我在设备上找不到任何新文件(我还尝试了 FilesystemDirectory 枚举中的所有值)。在 Android(7 和 10)和 IOS(13)上进行了测试。
我的 Blob 很好(我可以在浏览器中可视化它)并且 dataUrl 转换已正确完成。
因此,Capacitor FileSystem API 中的 writeFile 函数似乎无法将文件保存到设备上。
有谁知道为什么以及如何解决这个问题?我真的陷入困境,目前无法实现此功能。
这是我的依赖项和插件:
"dependencies": {
"@angular/animations": "^9.1.7",
"@angular/cdk": "^9.2.3",
"@angular/common": "^9.1.7",
"@angular/compiler": "^9.1.7",
"@angular/core": "^9.1.7",
"@angular/fire": "^5.4.2",
"@angular/forms": "^9.1.7",
"@angular/material": "^9.2.3",
"@angular/material-moment-adapter": "^9.2.4",
"@angular/platform-browser": "^9.1.7",
"@angular/platform-browser-dynamic": "^9.1.7",
"@angular/platform-server": "^9.1.7",
"@angular/router": "^9.1.7",
"@angular/service-worker": "^9.1.7",
"@auth0/angular-jwt": "^2.1.0",
"@capacitor/android": "^2.1.0",
"@capacitor/core": "2.1.0",
"@capacitor/ios": "^2.1.0",
"@ionic-native/core": "^5.25.0",
"@ionic-native/fcm": "^5.26.0",
"@ionic-native/http": "^5.26.0",
"@ionic-native/splash-screen": "^5.25.0",
"@ionic-native/status-bar": "^5.25.0",
"@ionic/angular": "^5.1.1",
"@ngrx/effects": "^9.1.2",
"@ngrx/entity": "^9.1.2",
"@ngrx/store": "^9.1.2",
"@ngrx/store-devtools": "^9.1.2",
"ajv": "^6.9.1",
"angular2-text-mask": "^9.0.0",
"animate.css": "^3.7.0",
"bootstrap-sass": "^3.4.0",
"capacitor-fcm": "^2.0.0",
"classlist.js": "^1.1.20150312",
"cordova-plugin-advanced-http": "^2.4.1",
"cordova-plugin-file": "^6.0.2",
"core-js": "^2.6.4",
"fibers": "^4.0.3",
"file-saver": "^2.0.2",
"firebase": "^5.8.2",
"font-awesome": "^4.7.0",
"html-webpack-plugin": "^2.21.0",
"jetifier": "^1.6.6",
"lottie-web": "^5.4.3",
"moment": "^2.24.0",
"ngx-markdown": "^9.0.0",
"pwa": "^1.9.6",
"rxjs": "^6.5.5",
"scrolling-element": "^1.0.2",
"tslib": "^1.10.0",
"web-animations-js": "^2.3.2",
"zone.js": "~0.10.2"
},
"devDependencies": {/**/},
"cordova": {
"plugins": {
"cordova-plugin-advanced-http": {}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
console.log方法的输出实际上可以给出有关文件系统错误导入的提示。
代替:
import { Filesystem } from '@capacitor/core';
Run Code Online (Sandbox Code Playgroud)
尝试:
const { Filesystem } = Plugins;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14608 次 |
| 最近记录: |