使用javascript在safari上保存对象(.xls文件)客户端

时间:2016-03-18 14:29:31

标签: javascript angularjs safari blob

在我的应用程序中,我从服务器接收一个大文件(在帖子响应中),我想使用javascript在客户端下载它。我想知道有一个适用于chrome的下载属性,并且有一个适用于IE的msSaveBlob函数。但它们都不适用于Safari。我该如何修理野生动物园?下面是我的代码:

$scope.saveAs = function (data, filename, type) {    
    if (angular.isUndefined(type)) {
    type = 'application/vnd.ms-excel';
}
var blob = new Blob([data], {'type': type});
if (angular.isDefined(window.navigator.msSaveBlob)) {
    // IE workaround for HTML7007
    window.navigator.msSaveBlob(blob, filename);
} else {
    var URL = window.URL || window.webkitURL;
    var downloadUrl = URL.createObjectURL(blob);

    if (filename) {
        // use HTML5 a[download] attribute to specify filename
        var a = document.createElement('a');
        // safari doesn't support this yet
        if (angular.isUndefined(a.download)) {
            window.location = downloadUrl;
        } else {
            a.href = downloadUrl;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }
    } else {
        window.location = downloadUrl;
    }

    setTimeout(function () {
        URL.revokeObjectURL(downloadUrl);
    }, 100); // cleanup
}

};

0 个答案:

没有答案