terikon / cordova-plugin-photo-library

Maintainer needed. Please contact if you're using this library in your project
MIT License
149 stars 298 forks source link

browser platform: saveImage and saveVideo should download file #38

Open viskin opened 7 years ago

wobsoriano commented 4 years ago

Here's what I did:

Check what platform it is, if it's running in a browser, download the file using this function:

export function downloadImageFromUrl(url, filename) {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  const img = new Image();
  img.crossOrigin = '';
  img.src = url;
  img.onload = function () {
    const img = this;
    canvas.width = img.width;
    canvas.height = img.height;
    ctx.drawImage(img, 0, 0, img.width, img.height);

    // Create hidden href
    const a = document.createElement('a');
    a.href = canvas.toDataURL();
    a.download = filename || `${Date.now()}.jpeg`;
    document.body.appendChild(a);
    a.click();
  };
}

I'm using this to download firebase storage files.