【问题标题】:ionic 3 - file download with FileTransfer not workingionic 3 - 使用 FileTransfer 下载文件不起作用
【发布时间】:2018-05-13 16:34:27
【问题描述】:

在一个角度/离子应用程序中,我有以下代码可以从互联网下载 pdf。

public pdfDownload(){
        const fileTransfer: FileTransferObject = this.transfer.create();
        const mime = 'application/pdf';
        const pdfFile = 'http://www.pdf995.com/samples/pdf.pdf';
        // alert(this.file.dataDirectory);
        fileTransfer.download(pdfFile, this.file.dataDirectory + 'file.pdf', true)
            .then((entry) => {
                alert('download complete: ' + entry.toURL());
            }, (error) => {
                // handle error
            });
    }

但现在我在我的 android 手机中寻找这个文件,但我找不到它。我手机里的路径是 没有文件。我在哪里下载的?

【问题讨论】:

标签: angular ionic-framework ionic3


【解决方案1】:

在第二个参数中使用 this.file.externalDataDirectory+'file.pdf' 而不是 this.file.dataDirectory+'file.pdf'函数文件Transfer.download(1参数,2参数,3参数);

例如

fileTransfer.download(pdfFile, this.file.externalDataDirectory + 'file.pdf', true)

【讨论】:

  • 相同.. 对我来说,它没有任何作用。仿佛承诺永远不会回来。甚至没有错误消息。
【解决方案2】:
  1. 文件保护程序下载方法
    async downloadFile() {
          await 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");
        }
  1. 获取用户安卓设备保存文件的权限方法
       getPermission() {
              this.androidPermissions.hasPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
                .then(status => {
                  if (status.hasPermission) {
                    this.downloadFile(); // your file transfer method 
                  } 
                  else {
                    this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE)
                      .then(status => {
                        if(status.hasPermission) {
                          this.downloadFile(); // your file transfer method
                        }
                      });
                  }
                });
            }
  1. 重要提示:

    他们中的许多人使用READ_EXTERNAL_STORAGE,这就是它不会存储您的文件的原因。你应该在权限中尝试WRITE_EXTERNAL_STORAGE

100% 它适用于离子 4^。

【讨论】:

    猜你喜欢
    • 2019-07-30
    • 2017-06-28
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多