Download File from URL

Hi I have connected my Ionic 2+ app to Amazon S3. In my app when I want to download a file my backend delivers a unique pre-signed URL generated from my S3. Looks something like this:

https://s3.ap-southeast-2.amazonaws.com/test%2F1/435345345-4545-4aa8-8a8a-etertertttrt-4addd157-ac38-41fd-5454-8ffa709ee77fcsdfsdfa...

My question is how do I download the file programatically through my component once I have the url? I have tried so far without luck:

window.open(url, '_blank');

And

getFile(url: string): void {
    this.http.get(url)
    .subscribe(blob => {
      console.log('Blob', blob);
      var data = new Blob([blob], { type: 'text/csv' });
      var url= window.URL.createObjectURL(data);
      window.open(url);
    }, error => {
        console.log(JSON.stringify(error.json()));
    });
    console.log('Get File http');
  }

Ok so further to above I sort of got it working for my iOS and android app by implementing cordova file transfer. However it does not work in a normal web browser as file transfer is mobile only. My code below.

this.fileTransfer.download(url, 'file.pdf').then((entry) => {
      console.log('download complete: ' + entry.toURL());
    }, (err) => {
      // handle error
      console.log('Error',err);
    });

However it opens the file in a new window. Is there anyway to actually give the user the option to save as? Also any work around to also work in a normal browser ie progressive web app?? Thanks in advance for any help on this.

3 Likes

how did you achieve it ? can you please elaborate and what all plugins you used here? for me it says ‘cannot find URL name’

Did you figure out how to make it work for normal browser(pwa) yet?

Not on the frontend but instead I had my backend dev to setup aws s3 to download as an attachment by apply content-disposition=>content as part of the file’s meta data. This way when I click on the href link it automatically downloads.

This article might help: http://iwantmyreal.name/s3-download-only-presigned-upload

1 Like