我正在尝试下载文件并将该文件保存在 iOS 中。我正在使用 ionic-native-file插件来实现这一点。该文件已下载,但我在设备中找不到该文件。
filewrite = (): void => {
let transfer = this.fileTransfer.create();
let path = "";
let dir_name = 'Download';
let file_name = "Sample.pdf";
if (this.platform.is('ios')) {
this.platform.ready().then(() => {
path = this.file.documentsDirectory;
this.file.writeFile(path,file_name,this.pdfSrc).then((entry) => {
this.showAlert("download completed");
}, (error) => {
this.showAlert("Download Failed.");
}).catch(err=>{
this.showAlert("Download Failed catch.");
this.showAlert(err.message);
});
}
)
}
};
下载完成的alert show,下载路径为:
file:///var/mobile/Containers/Data/Application/6DE22F30-5806-4393-830A-14C8A1F320BE/Library/Cloud/Sample.pdf
但我无法在设备中找到该位置。然后,我谷歌并看到here .
cordova.file.documentsDirectory - Files private to the app, but that are meaningful to other application (e.g. Office files). Note that for OSX this is the user's ~/Documents directory. (iOS, OSX)
因此,我实际上看不到该文件,因为它对应用程序是私有(private)的。然后我在同一个链接中看到:
因此,我尝试在 config.xml 中使用 both 首选项,但没有任何反应。
是否有任何方法可以将文件下载到 iOS 或保管箱或其他任何地方?
最佳答案
我也一直在努力寻找正确的目录来使用这个 cordova 插件在不同的平台上保存。我开始在 Android 上使用 file.externalRootDirectory
,在 iOS 上使用 file.cacheDirectory
。
正确的位置可能取决于您打算对文件执行的操作。在我的例子中,我只需要将它短期存储,这样我就可以使用用户的 native PDF 阅读器打开它。
关于ios - 如何在 iOS (Ionic 2/3) 中下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50737659/