加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何使用http在角度js中下载二进制文件

发布时间:2020-12-17 17:26:16 所属栏目:安全 来源:网络整理
导读:我想使用$http下载exe文件,它在我控制数据时显示我,但我无法下载它. $http({ url: url,method: "GET",headers: { 'Content-type': 'application/json' } }).success(function (data,status,headers,config) { console.log(data); window.open(objectUrl); })
我想使用$http下载exe文件,它在我控制数据时显示我,但我无法下载它.

$http({
        url: url,method: "GET",headers: {
           'Content-type': 'application/json'
        }
    }).success(function (data,status,headers,config) {
        console.log(data);

        window.open(objectUrl);
    }).error(function (data,config) {
        //upload failed
    });

任何帮助,将不胜感激.

解决方法

Given code will help you to download exe file as well as to check
browser compatibility.

var ieEDGE = navigator.userAgent.match(/Edge/g);
var ie = navigator.userAgent.match(/.NET/g); // IE 11+
var oldIE = navigator.userAgent.match(/MSIE/g); 

var blob = new window.Blob([data.data],{ type: 'application/x-msdownload' });

if (ie || oldIE || ieEDGE) {

    var fileName="filename"+'.exe';
    window.navigator.msSaveBlob(blob,fileName);
}
else {

    var file = new Blob([ data.data ],{
        type : 'application/x-msdownload'
    });

    var fileURL = URL.createObjectURL(file);
    var a         = document.createElement('a');
    a.href        = fileURL; 
    a.target      = '_blank';
    a.download    = "filename"+'.exe';
    document.body.appendChild(a);

    a.click();

}
//Successfully Downloaded

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读