Downloading Excel file xlsx in Angularjs and WebApi

后端 未结 5 792
离开以前
离开以前 2021-01-31 11:52


I am working on a task, in which I have to download a report in xlsx format. The report file is generated successfully from server, and is received on client side as well.

5条回答
  •  -上瘾入骨i
    2021-01-31 12:31

    I expect your $http call is missing the response type configuration. This is the way I download office files:

    function download(url, defaultFileName) {
        var self = this;
        var deferred = $q.defer();
        $http.get(url, { responseType: "arraybuffer" }).then(
            function (data, status, headers) {
                var type = headers('Content-Type');
                var disposition = headers('Content-Disposition');
                if (disposition) {
                    var match = disposition.match(/.*filename=\"?([^;\"]+)\"?.*/);
                    if (match[1])
                        defaultFileName = match[1];
                }
                defaultFileName = defaultFileName.replace(/[<>:"\/\\|?*]+/g, '_');
                var blob = new Blob([data], { type: type });
                saveAs(blob, defaultFileName);
                deferred.resolve(defaultFileName);                    
            }, function (data, status) {
                var e = /* error */
                deferred.reject(e);
            });
        return deferred.promise;
    }
    

提交回复
热议问题