1.通过a标签的方式来下载
<a href='' target='_blank'></a>
2.通过提交form表单的方式
var $form = $('<form action="'+url+'" method="post" target="rfFrame"></form>'); angular.forEach(d,function(v,k){ v = JSON.stringify(v); var $input = $('<input type="hidden" name="'+k+'"/>'); $input.attr('value',v); $form.append($input); }); $element.append($form); $form.submit(); $form.remove();
3.通过http的请求拿到文件流的方式
$http({ url : $scope.url, method : 'post', data : $scope.form.formdata, responseType : 'arraybuffer', }).then(function(d){ var data = d.data; var blob = new Blob([data], {type: "text/plain"}); //var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});//xlsx //var blob = new Blob([data], {type: "application/vnd.ms-excel"});//xls var downloadTitle = ''; if(d.headers('content-disposition').indexOf('filename') != -1){ downloadTitle = d.headers('content-disposition').split(';')[1].split('=')[1]; downloadTitle = decodeURI(downloadTitle); } if(typeof window.navigator.msSaveBlob !== 'undefined'){ window.navigator.msSaveBlob(blob, downloadTitle); }else{ var objectUrl = URL.createObjectURL(blob); var aForExcel = $("<a download='"+downloadTitle+"'><span class='forExcel'></span></a>").attr("href",objectUrl); $("body").append(aForExcel); $(".forExcel").click(); aForExcel.remove(); } });
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。