通过safari浏览器授权文档下载> Angular中的6个版本

时间:2015-10-05 09:21:29

标签: jquery angularjs jquery-plugins safari safari6

我想通过safari浏览器grater然后版本6下载我上传的文档。    我已成功下载IE,谷歌浏览器和火箱浏览器,但此代码不支持在agular js中授权下载文件

这是chrome,firebox和IE not safari浏览器中的代码

   $scope.ImageUrl = function (docId, filename) {
    $http({
        method: 'GET',
        cache: false,
        url: apiURL + "/CoachProfile/DownloadUserDocument/" + docId,
        responseType: 'arraybuffer',
        headers: {
            'Content-Type': 'application/json; charset=utf-8'
        }

    }).success(function (data, status) {
        console.log(data);
        var contentType = 'application/octet-stream';
        var success = false;


        try {

            if (typeof Blob !== "function") {
                console.log("Bob !function");
            }
            console.log(data);
            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);
            }

            success = true;
        } catch (ex) {
            console.log("Method failed with the following exception:");
            console.log(ex);
        }
        if (!success) {

            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  URL
                        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);
                        success = true;

                    } catch (ex) {
                        console.log("Download link method with simulated click failed with the following exception msg:");
                        console.log(ex);
                    }
                }

                if (!success) {

                    // Fallback to window.location method
                    try {
                        var blob = new Blob([data.buffer], { type: 'application/zip' })
                        var url = urlCreator.createObjectURL(blob);
                        window.location = url;
                        success = true;
                        console.log('safari browser does not support download');

                        var link = document.createElement('link');
                        //link.rel = 'stylesheet';
                        link.href = url;// window.URL.createObjectURL(blob);
                        //document.body.appendChild(link);


                    } 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(baseURL, '_blank', '');
            //safari.self.tab.dispatchMessage("open_new_tab", url);
        }


    }).error(function (data, status) {
        console.log("Request failed with status: " + status);

    });
}

如果您对此有任何疑问,请帮助我累了尝试通过safari浏览器下载文档

0 个答案:

没有答案