vatExportService.js 5.35 KB
Newer Older
sam.x.wang's avatar
sam.x.wang committed
1
webservices.factory('vatExportService', ['$log', '$http', 'apiConfig', '$q','vatSessionService', function ($log, $http, apiConfig, $q ,vatSessionService) {
2 3 4 5
    'use strict';
    $log.debug('vatExportService.ctor()...');

    //下载服务器上的xls文件
kevin's avatar
#  
kevin committed
6
    var exportToExcel = function (data, status, headers, defaultFileName, noExtendFileName) {
7 8 9
        $('#busy-indicator-container').show();

        var defer = $q.defer();
sherlock's avatar
sherlock committed
10
        var octetStreamMime = 'application/vnd.ms-excel';
11 12 13 14 15 16
        var success = false;

        // Get the headers
        headers = headers();

        // Get the filename from the x-filename header or default to "download.bin"
kevin's avatar
#  
kevin committed
17
        var filename = null;
sam.x.wang's avatar
sam.x.wang committed
18 19 20
        if (noExtendFileName) {
            filename = defaultFileName+ "_" +vatSessionService.project.name+'.xls'
        } else {
sam.x.wang's avatar
sam.x.wang committed
21
            filename = defaultFileName+"_"+decodeURI(headers['x-file-name']).replace('???','').replace(',','')+ "_" +vatSessionService.project.name+'.xls'
kevin's avatar
#  
kevin committed
22 23
        }

24 25 26 27 28 29 30

        // 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 ...");
sam.x.wang's avatar
sam.x.wang committed
31
            var blob = new Blob([data], {type: contentType});
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            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 ...");
sam.x.wang's avatar
sam.x.wang committed
58
                        var blobdownload = new Blob([data], {type: contentType});
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                        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 ...");
sam.x.wang's avatar
sam.x.wang committed
85
                        var blobsuccess = new Blob([data], {type: octetStreamMime});
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
                        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
106
        // todo deleteFile(encodeURI(filename));
107 108 109 110 111 112 113 114 115 116 117 118 119
        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) {
sam.x.wang's avatar
sam.x.wang committed
120
            return $http.post('/Report/export', {ReportData: jsonData}, apiConfig.createVat());
121 122 123
        }
    }
}]);