ios - 如何在 iOS (Ionic 2/3) 中下载文件?

标签 ios file ionic-framework ionic3

我正在尝试下载文件并将该文件保存在 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)的。然后我在同一个链接中看到:

enter image description here

因此,我尝试在 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/

相关文章:

ios - 每个顶点颜色的 SceneKit

android - 无法解析配置 ':_armv7DebugCompile' 的所有依赖项

ios - 如何更改 Ionic 2 iOS 应用程序的位置访问描述?我正在为我的应用程序使用 cordova 地理定位插件

ios - 适用于 iPhone X 的原始深度图 SDK

ios - avqueueplayer 在后台模式下播放问题

android - 如何打开文件浏览器? (安卓开发工具包)

java - 扫描仪不读取文件内容

python - 在 Python 中节省内存。如何迭代行并使用 200 万行文件有效地保存它们?

javascript - ionic 模态 slider 问题 : could not find a corresponding element with delegate-handle= on $emit

ios - 什么 iOS 版本支持 NSTimer 有效属性?