首页 > 解决方案 > 无法在离子 5+ 中使用离子文件传输插件下载文件

问题描述

在这里我使用离子文件传输插件下载文件,当我尝试下载文件时,我得到以下错误

401

而且每次我构建一个文件时,我还需要在android中添加以下代码

public class FileProvider extends androidx.core.content.FileProvider

这个下载还有其他选择吗?我检查了电容器站点,但我不确定如何在其中使用文件存储插件,下面是我使用文件传输的代码

 pdfUrl = 'https://www.cs.toronto.edu/~hinton/absps/NatureDeepReview.pdf';


  constructor(private transfer: FileTransfer, private file: File) {}

  download() {
    const fileTransfer: FileTransferObject = this.transfer.create();
    const url = this.pdfUrl;
  fileTransfer.download(url, this.file.dataDirectory).then((entry) => {
    console.log('download complete: ' + entry.toURL());
    this.saveData = entry.toURL();

  }, (error) => {
    // handle error
    console.log(error);
  });

标签: javascriptangularionic-frameworkionic4capacitor

解决方案


在 Android 上,cordova-plugin-file-transfer需要cordova-plugin-whitelist.

安装它npm install cordova-plugin-whitelist然后运行npx cap update

此外,cordova-plugin-file-transfer需要您要写入的文件的路径,您传递了一个目录,您应该更改fileTransfer.download(url, this.file.dataDirectory)fileTransfer.download(url, this.file.dataDirectory + '/NatureDeepReview.pdf')


推荐阅读