webservices.factory('vatExportService', ['$log', '$http', 'apiConfig', '$q', function ($log, $http, apiConfig, $q) { 'use strict'; $log.debug('vatExportService.ctor()...'); //下载服务器上的xls文件 var exportToExcel = function (data, status, headers, defaultFileName, noExtendFileName) { $('#busy-indicator-container').show(); var defer = $q.defer(); var octetStreamMime = 'application/vnd.ms-excel'; var success = false; // Get the headers headers = headers(); // Get the filename from the x-filename header or default to "download.bin" var filename = null; if(noExtendFileName){ filename = defaultFileName }else{ filename = decodeURI(headers['x-file-name']) + defaultFileName } // 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) { $log.debug("saveBlob method failed with the following exception:"); $log.debug(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 blobdownload = new Blob([data], { type: contentType }); var urldownload = urlCreator.createObjectURL(blobdownload); link.setAttribute('href', urldownload); // 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) { $log.debug("Download link method with simulated click failed with the following exception:"); $log.debug(ex); $q.reject(); } } 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 blobsuccess = new Blob([data], { type: octetStreamMime }); var urlsuccess = urlCreator.createObjectURL(blobsuccess); window.location = urlsuccess; //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:"); $log.debug(ex); $q.reject(); } } } } if (!success) { // Fallback to window.open method $log.debug("No methods worked for saving the arraybuffer, using last resort window.open"); window.open(httpPath, '_blank', ''); } //Delete the file // todo deleteFile(encodeURI(filename)); defer.resolve('success'); $('#busy-indicator-container').hide(); return defer.promise; }; //下载完成后将服务器文件删除 var deleteFile = function (filename) { return $http.get('/inputInvoiceImport/delete/file?filename=' + filename, apiConfig.createVat()); }; return { exportToExcel: exportToExcel, exportReport: function (jsonData) { return $http.post('/Report/export', { ReportData: jsonData }, apiConfig.createVat()); } } }]);