【问题标题】:Ionic 3 - download a file to directoryIonic 3 - 将文件下载到目录
【发布时间】:2018-03-23 08:07:08
【问题描述】:

我也有类似的问题: How to download file to Download's directory with Ionic Framework?

我在下载后收到成功警报,但在成功下载后显示的路径下的 Android 文件资源管理器中看不到该文件:file:///data/user/0/io.ionic.fileTest/image.jpg

我的代码:

download(){

const fileTransfer: FileTransferObject = this.transfer.create();
const url = "http://cdna.allaboutvision.com/i/conditions-2016/heterochromia-kate-bosworth-660x660-with-credit.jpg";

fileTransfer.download(url, this.file.dataDirectory + 'laska.jpg', true).then((entry) => {

  const alertSuccess = this.alertCtrl.create({
    title: `Download Succeeded!`,
    subTitle: `was successfully downloaded to: ${entry.toURL()}`,
    buttons: ['Ok']
  });
  alertSuccess.present();

}, (error) => {

  const alertFailure = this.alertCtrl.create({
    title: `Download Failed!`,
    subTitle: `was not successfully downloaded. Error code: ${error.code}`,
    buttons: ['Ok']
  });
  alertFailure.present();
});
}

我可以设法将这个文件保存在“下载”文件夹或“文档”中吗?我还尝试将目标路径更改为:

cordova.file.externalRootDirectory + '/Download/'

在这种情况下,我收到错误 1。

在许多示例中,我看到人们使用 window.requestFileSystem() 但看起来窗口对我来说没有这种方法。我使用 Visual Studio 代码和 ionic 3。

【问题讨论】:

    标签: javascript android cordova ionic-framework ionic3


    【解决方案1】:

    fileTransfer.download 有一点点错误

    this.file.dataDirectory代替this.file.applicationStorageDirectory

    【讨论】:

    • 我也试过了。问题是,如果我使用 dataDirectory,我会得到下载成功但文件在 android 文件资源管理器中不可用的信息。当我使用 applicationStorageDirectory 我收到错误 1
    • @bambaniasz 如果您找到任何解决方案,我们可以将文件存储在 ionic 的 Downloads 文件夹中吗?
    • @NarendraSinghRathore 你需要先获得许可。
    • @bambaniasz 已经请求加载,但仍然无法存储。
    【解决方案2】:

    将文件下载到下载目录的工作代码:

    downloadFile() {
      this.fileTransfer.download("https://cdn.pixabay.com/photo/2017/01/06/23/21/soap-bubble-1959327_960_720.jpg", this.file.externalRootDirectory + 
      '/Download/' + "soap-bubble-1959327_960_720.jpg").then()
    }
    
    getPermission() {
      this.androidPermissions.hasPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
        .then(status => {
          if (status.hasPermission) {
            this.downloadFile();
          } 
          else {
            this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE)
              .then(status => {
                if(status.hasPermission) {
                  this.downloadFile();
                }
              });
          }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多