AngularJS:从服务器下载pdf文件

时间:2014-08-25 16:07:40

标签: javascript angularjs

我想使用$http从网络服务器下载pdf文件。我使用这个效果很好的代码,我的文件只保存为html文件,但是当我打开它时,它会以pdf格式打开但在浏览器中。我在Chrome 36,Firefox 31和Opera 23上测试过它。

这是我的angularjs代码(based on this code):

UserService.downloadInvoice(hash).success(function (data, status, headers) {
                    var filename,
                        octetStreamMime = "application/octet-stream",
                        contentType;

                    // Get the headers
                    headers = headers();

                    if (!filename) {
                        filename = headers["x-filename"] || 'invoice.pdf';
                    }

                    // Determine the content type from the header or default to "application/octet-stream"
                    contentType = headers["content-type"] || octetStreamMime;

                    if (navigator.msSaveBlob) {
                        var blob = new Blob([data], { type: contentType });
                        navigator.msSaveBlob(blob, filename);
                    } else {
                        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) {
                                // Prepare a blob URL
                                var blob = new Blob([data], { type: contentType });
                                var url = urlCreator.createObjectURL(blob);

                                link.setAttribute("href", url);
                                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);
                            } else {
                                // Prepare a blob URL
                                // Use application/octet-stream when using window.location to force download
                                var blob = new Blob([data], { type: octetStreamMime });
                                var url = urlCreator.createObjectURL(blob);
                                $window.location = url;
                            }
                        }
                    }
                }).error(function (response) {
                    $log.debug(response);
                });

在我的服务器上,我使用Laravel,这是我的回复:

$headers = array(
                'Content-Type' => $contentType,
                'Content-Length' => strlen($data),
                'Content-Disposition' => $contentDisposition
            );

            return Response::make($data, 200, $headers);

其中$contentTypeapplication/pdf$contentDispositionattachment; filename=" . basename($fileName) . '"'

$ filename - 例如59005-57123123.PDF

我的回复标题:

Cache-Control:no-cache
Connection:Keep-Alive
Content-Disposition:attachment; filename="159005-57123123.PDF"
Content-Length:249403
Content-Type:application/pdf
Date:Mon, 25 Aug 2014 15:56:43 GMT
Keep-Alive:timeout=3, max=1

enter image description here

我做错了什么?

2 个答案:

答案 0 :(得分:1)

要指示浏览器下载文件而不是在浏览器中显示该文件,请尝试将以下标题添加到服务器响应中:

Content-Disposition:attachment; filename="filename.xxx"
Content-Type:application/octet-stream

答案 1 :(得分:1)

默认开放式应用程序:

根据屏幕截图,您似乎已将Firefox设置为在该计算机上打开PDF文档的默认应用程序。它使用它的嵌入式查看器。

因此它以PDF格式保存,但您的开放式应用程序首选项使其显示为Firefox Html文档。

如果您将打开的首选项更改回Adobe Reader,则应在浏览器中正确显示“另存为”类型。

我希望有所帮助。