离子3 - 文件下载,FileTransfer无法正常工作

时间:2017-11-29 21:02:13

标签: angular ionic-framework ionic3

在角度/离子应用程序中,我有以下代码从互联网上下载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手机中寻找这个文件,但我找不到它。我手机中的路径为enter image description here 没有文件。我从哪里下载的?

2 个答案:

答案 0 :(得分:1)

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

e.g。

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

答案 1 :(得分:0)

  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. 获取用于在用户Android设备中保存文件的权限方法
       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 ^上工作。