在Ionic中文件的位置在哪里下载 [英] where was file location downloaded in Ionic

查看:62
本文介绍了在Ionic中文件的位置在哪里下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码在ionic5中下载文件

I'm try to download file in ionic5 using this code

downloadImages() {
let photo_name = this.util.getPhotoName(this.contacts.rows.item(this.counter).person_fullname);
let url = encodeURI(this.imagePathUrl + photo_name + "?r=" + Math.round(Math.random() * 999999));
let local_path = this.util.getLocalPath();
let fileUrl = local_path + this.paramPro.getPhotoFileName() + "/" + photo_name;
this.fileTransfer.download(url, fileUrl).then(entry => {
console.log('download : ', entry);
  if (this.counter == this.contacts.rows.length-1) {
    this.loading.dismiss().catch(() => {});
    this.presentDownloadFinishAlert(this.paramPro.getMessage('title'), this.paramPro.getMessage('download_complete'));
  } else {
    this.counter++;
    this.downloadImages();
  }
}).catch(e => {
  if (this.counter == this.contacts.rows.length-1) {
    this.loading.dismiss().catch(() => {}); 
    this.presentDownloadFinishAlert(this.paramPro.getMessage('title'), this.paramPro.getMessage('download_complete'));
  } else {
    this.counter++;
    this.downloadImages();
  }
});

}

我得到了console.log

and i got console.log

file:///data/user/0/io.ionic.starter/files/photo_mg/%E0%B8%99%E0%B8%B2%E0%B8%A2%E0%B8%9B%E0%B8%B4%E0%B8%A2%E0%B8%81%E0%B8%A3%20%E0%B8%AD%E0%B8%A0%E0%B8%B4%E0%B8%9A%E0%B8%B2%E0%B8%A5%E0%B8%A8%E0%B8%A3%E0%B8%B5.png

>在以下位置下载的文件的位置相同使用离子的android 我也将此代码放在config.xml

it is same issue as Location of the file downloaded on android using ionic i also put this code in config.xml

<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />

并将此代码放入AndroidManifest.xml

and put this code in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

手机中的file:///data/user/0/io.ionic.starter/files仍然不起作用?我在Windows资源管理器中找到了文件,但没有找到.

it still doesn't work where was file:///data/user/0/io.ionic.starter/files in my phone ? I find it with windows explorer find files, and i not found it.

推荐答案

不是特别适合您的需求,但我可以提供更大的解决方案.我为Ionic创建了一个缓存服务,其中包括文件保存和检查

Not especially for your need but I can provide a larger solution. I create a cache service for Ionic and it includes file saving and checking

  private win: any = window;

  public async checkFile(fileUrl) {

    let stringURL = String(fileUrl);
    let fileName = stringURL.split('//')[1].replace(/\//g, '-');
    let directory;

    if (this.platform.is("desktop")) {
      return fileUrl;
    }
    if (this.platform.is("android")) {
      directory = this.file.dataDirectory;
    }
    if (this.platform.is("ios")) {
      directory = this.file.dataDirectory;
    }
    return this.file.checkFile(directory, fileName)
      .then(
        async result => {
          if (result) {
            return this.win.Ionic.WebView.convertFileSrc(directory + fileName);
          } else {
            return fileUrl;
          }
        }
      )
      .catch(
        async err => {
          //FileError.NOT_FOUND_ERR
          if (err.code == 1) {
            let fileTransfer: FileTransferObject = this.transfer.create();
            fileTransfer.download(fileUrl, directory + fileName, true)
              .then(
                result => {
                }
              )
              .catch(
                error => {
                  console.error(error);
                  throw error;
                }
              )
          }
          return fileUrl;

        }
      )


  }

如果您在checkFile函数中登录目录或导致该函数的catch部分,您将看到文件所在的位置

If you log directory in checkFile function or result in catch part of the function you will see where the file is

您可以从此处检查项目和代码: https://gitlab.com/demarkelabs/sanalmarketuygulamam/ionic-app/-//blob/master/src/app/cache.service.ts

You can check project and code from here : https://gitlab.com/demarkelabs/sanalmarketuygulamam/ionic-app/-/blob/master/src/app/cache.service.ts

这篇关于在Ionic中文件的位置在哪里下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆