0

Is there any way I can intercept the call initiated from the a href tag and read the content from indexed db and download the file.

Scenario I Am designing an offline application when users asked to get content for offline. When user clicks the href tag,

<a href='http://downlod.com/downloaddcoument'>download</a>

it should read the response saved in indexed db and download the file, In service worker the fetch option not able to track this call so am not able to inten

I tried with $http.get and am getting the response but not able to download the file.

Any link of hint will be a great help, thank you

Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27
Aji
  • 423
  • 8
  • 23
  • Have you checked this related [SO post](https://stackoverflow.com/questions/21607309/how-to-download-file-using-anchor-tag-a)? This will be like: `download`. – Jessica Rodriguez Sep 25 '18 at 10:51
  • no my issue is not downloading. its how i can intercept that and make it read from indexed db when am offline – Aji Sep 25 '18 at 12:17

1 Answers1

0

For that you have make the download call like this then the fetch event will trigger and you can push the response to Indexed db and when offline you can read from the indexed db

downloadfile() {
        var self = this;
        var getComment = 'http://localhost/CommentService/api/Products/GetbookFor?format=xlsx';
        this.httpservice.get(getComment).then(function (response) {
            self.saveData(response.data, "sampleFile.xlsx");
        });  
    }

saveData = (function () {
    var a = document.createElement("a");
    document.body.appendChild(a);
    // a.style = "display: none";
    return function (data, fileName) {
        var json = JSON.stringify(data),
            blob = new Blob([json], { type: "octet/stream" }),
            url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = fileName;  
        a.click();
        window.URL.revokeObjectURL(url);
    };
}());
Aji
  • 423
  • 8
  • 23