angular-file-saver使用FileSaver下载base64文件 [英] angular-file-saver download base64 file using FileSaver

查看:369
本文介绍了angular-file-saver使用FileSaver下载base64文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to download a file that is base64 using angular-file-saver.

我只需使用以下html标记就可以在不使用angular-file-saver的情况下做到这一点:

I can do this without angular-file-saver with just this html mark-up:

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

我现在还有其他需要用angular-file-saver满足的需求,这些需求使我过渡到使用FileSaver进行此操作.现在,我想使用文件保护程序来实现相同的下载.我的html标记是:

I have other needs now that are fulfilled with angular-file-saver that are causing me transition to doing this with FileSaver. Now I want to implement the same download using file saver. My html mark-up is:

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

然后我建立如下的downloadFile函数:

Then I build up my downloadFile function like this:

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

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

My issue is that after the file downloads when I attempt to open it the file is corrupt.

我假设通过连接; base64"对类型对象做错了.我已经开始研究angular-file-saver.bundle.js,但是对您的帮助非常感谢.我在做什么错了?

I'm assuming that I'm doing something wrong with the type object by concatenating ";base64". I've started digging into angular-file-saver.bundle.js but any help is greatly appreciated. What am I doing wrong?

推荐答案

我最终研究了BLOB,并遇到了这个stackoverflow以使其工作的情况. 根据JavaScript中的base64字符串创建Blob

I ended up digging into BLOB and came across this stackoverflow to get it working. Creating a Blob from a base64 string in JavaScript

这篇关于angular-file-saver使用FileSaver下载base64文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆