javascript - 无法在 ionic 5+ 中使用 ionic 文件传输插件下载文件

标签 javascript angular ionic-framework ionic4 capacitor

在这里我使用 ionic 文件传输插件下载文件,当我尝试下载文件时,我得到以下错误
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);
  });

最佳答案

在 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')

关于javascript - 无法在 ionic 5+ 中使用 ionic 文件传输插件下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68049352/

相关文章:

javascript - 指令链接函数中未定义的范围变量

html - 等高 flex 行不起作用

javascript - 将值提交到小数点后 2 位

javascript - 在矩形、圆形和多边形内填充标记

javascript - "Object doesn' t 支持此属性或方法"IE10/11

javascript - 使用键码(箭头)停止窗口滚动,event.preventDefault() 不起作用?

html - 线不会因 Angular 而中断\n

Angular 、 Material 侧导航和粘性工具栏

javascript - 如何使用 fromTemplateUrl 的新变体将参数传递给 Ionic 模式?

javascript - Angular FormGroup - 自动将另一个字段的值除以 100