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