【问题标题】:angular-file-saver download base64 file using FileSaverangular-file-saver 使用 FileSaver 下载 base64 文件
【发布时间】:2015-10-27 05:19:40
【问题描述】:

我正在尝试使用 angular-file-saver 下载 base64 文件。

我可以在没有 angular-file-saver 的情况下使用这个 html 标记来做到这一点:

<a ng-href="data:{{document.mimeType}};base64,{{document.base64Code}}" target="_blank" download>Download Single Document</a>

我现在有其他需求可以通过 angular-file-saver 满足,这导致我过渡到使用 FileSaver 执行此操作。现在我想使用文件保护程序实现相同的下载。我的 html 标记是:

<a ng-href="#" ng-click="downloadFile()">Download with File Saver</a>

然后我像这样构建我的 downloadFile 函数:

function downloadFile () {
        var data = new blob([$scope.document.base64Code], {type: $scope.document.mimeType+';base64'});
        var config = {
            data: data,
            filename: $scope.documentSaveAs ? $scope.documentSaveAs : $scope.document.FileName
        }
        fileSaver.saveAs(config);
    }

我的问题是,当我尝试打开文件下载文件后,文件已损坏。

我假设我通过连接“;base64”对类型对象做错了什么。我已经开始深入研究 angular-file-saver.bundle.js,但非常感谢任何帮助。我做错了什么?

【问题讨论】:

    标签: angularjs filesaver.js


    【解决方案1】:

    我最终深入研究了 BLOB,并遇到了这个 stackoverflow 来让它工作。 Creating a Blob from a base64 string in JavaScript

    【讨论】:

    • 只是为了添加答案,我建议将 npm 验证器添加到您的解决方案中。您可以在尝试下载或转换为 blob 之前验证您的 sting 是否为 base64。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2016-11-13
    • 1970-01-01
    • 2018-02-06
    • 2018-07-23
    • 2021-04-24
    • 2019-12-14
    相关资源
    最近更新 更多