/// module Configs{ export function removeElementByValue(array:Array ,value:any, key?:string ){ if(key){ for(var i=0; i, elements:Array){ angular.forEach(elements, (element) =>{ removeElementByValue(array, element.value, element.key); }); } /** 对象的深拷贝 */ export function deepCopy(object) { var n,i; if (object instanceof Array) { n = []; for (i = 0; i < object.length; ++i) { n[i] = deepCopy(object[i]); } return n; } else if (object instanceof Object) { n = {} for (i in object) { n[i] = deepCopy(object[i]); } return n; } else { return object; } } export function downloadFile($scope, $http, url, fn?){ $http.get(url, { responseType: 'arraybuffer'}) .success( function(data, status, headers) { if(angular.isFunction(fn)) fn(); var octetStreamMime = 'application/octet-stream'; var success = false; // Get the headers headers = headers(); // Get the filename from the x-filename header or default to "服务列表" var filename = "服务文件"; var params = headers['content-disposition'].split(";"); for(var i = 0; i< params.length; i++){ if(params[i].indexOf("filename") != -1){ var tmp = params[i].split("="); if(tmp.length == 2) filename = tmp[1].replace(new RegExp("\"", "gm"),""); } } // Determine the content type from the header or default to "application/octet-stream" var contentType = headers['content-type'] || octetStreamMime; try{ // Try using msSaveBlob if supported console.log("Trying saveBlob method ..."); var blob = new Blob([data], { type: contentType }); if(navigator.msSaveBlob) navigator.msSaveBlob(blob, filename); else { // Try using other saveBlob implementations, if available var saveBlob = navigator["webkitSaveBlob"] || navigator["mozSaveBlob"] || navigator["saveBlob"]; if(saveBlob === undefined) throw "Not supported"; saveBlob(blob, filename); } console.log("saveBlob succeeded"); success = true; } catch(ex){ console.log("saveBlob method failed with the following exception:"); console.log(ex); } if(!success){ // Get the blob url creator var urlCreator = window.URL || window["webkitURL"] || window["mozURL"] || window["msURL"]; if(urlCreator){ // Try to use a download link var link = document.createElement('a'); if('download' in link){ // Try to simulate a click try{ // Prepare a blob URL console.log("Trying download link method with simulated click ..."); var blob = new Blob([data], { type: contentType }); var url = urlCreator.createObjectURL(blob); link.setAttribute('href', url); // Set the download attribute (Supported in Chrome 14+ / Firefox 20+) link.setAttribute("download", filename); // Simulate clicking the download link var event = document.createEvent('MouseEvents'); event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); link.dispatchEvent(event); console.log("Download link method with simulated click succeeded"); success = true; }catch(ex) { console.log("Download link method with simulated click failed with the following exception:"); console.log(ex); } } if(!success){ // Fallback to window.location method try{ // Prepare a blob URL // Use application/octet-stream when using window.location to force download console.log("Trying download link method with window.location ..."); var blob = new Blob([data], { type: octetStreamMime }); var url = urlCreator.createObjectURL(blob); window.location = url; console.log("Download link method with window.location succeeded"); success = true; }catch(ex){ console.log("Download link method with window.location failed with the following exception:"); console.log(ex); } } } } if(!success){ // Fallback to window.open method console.log("No methods worked for saving the arraybuffer, using last resort window.open"); window.open('_blank', ''); Configs.customAlert("提示", "文件下载失败", '',null, 0, "error"); } }).error(function(data, status) { console.log("Request failed with status: " + status); // Optionally write the error out to scope fn(); Configs.customAlert("提示", "文件下载失败", '',null, 0, "error"); $scope.errorDetails = "Request failed with status: " + status; }); } }