移动设备中的Angularjs PDF下载不起作用?

移动设备中的Angularjs PDF下载不起作用?,angularjs,pdf,Angularjs,Pdf,这是我的下载网址: http://xxx.xxx.xxx.xxx/gk/panel/user/index/export/type/pdf/qid/238/formData/{"users":{"firstname":"dhamskar"}} 这是我的控制器: $scope.downloadPDF = function() { // Use an arraybuffer $http.get('http://xxx.xxx.xxx.xxx/

这是我的下载网址:

http://xxx.xxx.xxx.xxx/gk/panel/user/index/export/type/pdf/qid/238/formData/{"users":{"firstname":"dhamskar"}}
这是我的控制器:

$scope.downloadPDF = function() {
                // Use an arraybuffer
             $http.get('http://xxx.xxx.xxx.xxx/gk/panel/user/index/export/type/pdf/qid/238/formData/{"users":{"firstname":"dhamskar"}}', { responseType: 'arraybuffer' })
                .success( function(data, status, headers) {

                var octetStreamMime = 'application/pdf';
                var success = false;

                // Get the headers
                headers = headers();

                // Get the filename from the x-filename header or default to "download.bin"
                var filename = headers['Content-Disposition'] || group_name+'_export_user_management.pdf';

                // 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)
                {
                    console.log("saveBlob method failed with the following exception:");
                    console.log(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 blob = new Blob([data], { type: contentType });
                                var url = urlCreator.createObjectURL(blob);

                                var url1 =  $sce.trustAsResourceUrl(url);
                                console.log("print url:"+url1);
                                link.setAttribute('href', url1);

                                // 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) {
                                console.log("Download link method with simulated click failed with the following exception:");
                                console.log(ex);
                            }
                        }

                        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 blob = new Blob([data], { type: octetStreamMime });
                                var url = urlCreator.createObjectURL(blob);
                                var url1 =  $sce.trustAsResourceUrl(url);
                                console.log("print url:"+url1);
                                window.location = url1;
                                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:");
                                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(url1, '_blank', '');
                }
            })
            .error(function(data, status) {
                console.log("Request failed with status: " + status);

                // Optionally write the error out to scope
                $scope.errorDetails = "Request failed with status: " + status;
            });
    };

使用以下步骤进行故障排除:

  • 相对路径而不是绝对路径
  • 还是作为一种服务