使用FileTransfer下载Ionic 4

时间:2019-03-08 08:58:13

标签: android angular cordova ionic-framework ionic4

import { Component, OnInit } from '@angular/core';
import { ModalController, Platform } from '@ionic/angular';
import { PushdataService } from '../pushdata.service';
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/file/ngx';

@Component({
  selector: 'app-modalquotes',
  templateUrl: './modalquotes.page.html',
  styleUrls: ['./modalquotes.page.scss'],
})
export class ModalquotesPage implements OnInit {

  public storageDirectory:any;

  constructor(private transfer: FileTransfer, private file: File, private modalCtrl: ModalController, public pushdata: PushdataService) {
  }


  downloadme() {

    const fileTransfer: FileTransferObject = this.transfer.create();
      this.storageDirectory = this.file.externalRootDirectory + 'Download/';

      const url = "https://xxxxx.com/server_api/asset/quotes/test.jpeg";
      fileTransfer.download(url,this.storageDirectory + "test.jpeg", true).then(
          (entry) => {
            alert('Berhasil download dan tersimpan di : ' + this.storageDirectory + "test.jpeg");
          }, (error) => {
            alert('gagal : ' + JSON.stringify(error));
          });

  }


  ngOnInit() {
  }

}

这是我的代码page.ts,但是下载的结果是“ Permitte Denied”。请帮助我,我有很多尝试,但有一些错误。这仅适用于"this.file.externalApplicationStorageDirectory",但只能从根文件夹应用程序下载文件。但是我希望将此图像下载到文件夹Download。

1 个答案:

答案 0 :(得分:0)

首先,externalRootDirectory属性用于将文件存储到外部存储设备(例如SD卡)。

所以我建议您更改以下代码行:

this.storageDirectory = this.file.externalRootDirectory + 'Download/';

          TO

this.storageDirectory = 'Download/';

这应该将您的文件下载到“下载”文件夹中