【问题标题】:Error when trying to download image on android (ionic v4)尝试在 android (ionic v4) 上下载图像时出错
【发布时间】:2020-02-11 12:14:16
【问题描述】:

我正在使用 ionic v4 和 ngx-qrcode 库来生成 qrcode。但是我还需要下载这个生成的 qrcode,但是当我点击 android 上的按钮时,ionic 不会执行任何操作。在浏览器中工作。

getImage(): void {
    const canvas = document.querySelector('canvas') as HTMLCanvasElement;

    const imageData = canvas.toDataURL('image/jpeg').toString();
    const img = document.createElement('img');
    img.src = imageData;

    const a = document.createElement('a');
    a.setAttribute('download', 'YourFileName.jpeg');
    a.setAttribute('href', imageData);
    a.appendChild(img);
    a.click();
  }

当我单击 html 中的下载按钮时,它仅在我在浏览器中时才有效,但在 android 中没有任何反应

【问题讨论】:

标签: angular ionic-framework download qr-code


【解决方案1】:

不能使用带有下载属性的链接,需要使用cordova的file原生插件。我分享这段代码:

import {File} from '@ionic-native/file/ngx';

save(){
  this.file.writeFile(this.file.applicationStorageDirectory, name, data)
    .then((entry)=>{
        console.log(entry);
    }).catch((err)=>{
        console.log(err);
  })
}

Here关于存储文件的信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多