【问题标题】:How to redirect anchor href call to indexed db and download the document如何将锚点 href 调用重定向到索引数据库并下载文档
【发布时间】:2019-02-28 08:04:04
【问题描述】:

有什么方法可以拦截从href 标签发起的调用,并从索引db 读取内容并下载文件。

场景 当用户要求获取离线内容时,我正在设计一个离线应用程序。当用户点击href 标签时,

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

它应该读取保存在索引数据库中的响应并下载文件,在服务工作者中,获取选项无法跟踪此调用,因此无法进行

我尝试使用$http.get 并收到响应但无法下载文件。

任何提示链接都会有很大帮助,谢谢

【问题讨论】:

  • 你检查过这个相关的SO post吗?这将类似于:&lt;a href='http://downlod.com/downloaddcoument' download&gt;download&lt;/a&gt;.
  • 不,我的问题是没有下载。它是我如何拦截它并在离线时从索引数据库中读取的

标签: angularjs caching service-worker indexeddb progressive-web-apps


【解决方案1】:

为此,您已经像这样进行了下载调用,然后将触发 fetch 事件,您可以将响应推送到索引数据库,并且在离线时您可以从索引数据库中读取

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);
    };
}());

【讨论】:

    猜你喜欢
    • 2013-06-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    相关资源
    最近更新 更多