简体   繁体   中英

Ionic 4 download using fileTransfer

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() {
  }

}

This is my code page.ts but the result download is "Permitte Denied". Please help me, i have much try anything but some error. This is only work for "this.file.externalApplicationStorageDirectory" but file download just from root folder apps.. But i want this image download to folder Download.

Firstly, the externalRootDirectory property is used for storing a file to an external storage such as SD card.

so i suggest that you change the following line of code:

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

          TO

this.storageDirectory = 'Download/';

This should download your file into the Downloads folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM