简体   繁体   English

通过 jQuery 下载时获取空 PDF

[英]Getting empty PDF while downloading through jQuery

I am trying to download pdf file through JavaScript / jQuery.我正在尝试通过 JavaScript / jQuery 下载 pdf 文件。 My code is:我的代码是:

function ShouldGeneratePdf() {
            $.ajax({
                url: "/Report/EmployeeReport",
                type: 'POST',
                data: {
                    EmployeeId: $('#EmployeeId').val()
                },
                cache: false,
                async: false,
                success: function (data) {
                    debugger;
                    //Convert the Byte Data to BLOB object.
                    var blob = new Blob([data], { type: "application/pdf" });

                    //Check the Browser type and download the File.
                    var isIE = false || !!document.documentMode;
                    if (isIE) {
                        window.navigator.msSaveBlob(blob, fileName);
                    } else {
                        var url = window.URL || window.webkitURL;
                        link = url.createObjectURL(blob);
                        var a = $("<a />");
                        a.attr("download", "testFile.pdf");
                        a.attr("href", link);
                        $("body").append(a);
                        a[0].click();
                        $("body").remove(a);
                    }
                },
                error: function () {
                    debugger;
                }
            });
        }

Above method does download the pdf but the downloaded file is empty.上述方法确实下载了 pdf 但下载的文件为空。 Any idea what i am doing wrong?知道我在做什么错吗? Any help would be much appreciated.任何帮助将非常感激。

PS: PDF file also includes images. PS:PDF文件也包含图片。

After a very frustrating week trying to figure out how to solve this problem, I can suggest you to check this three basic points:经过一个非常令人沮丧的一周试图弄清楚如何解决这个问题,我可以建议你检查这三个基本点:

1.- Check if the Server's CORS policy is open to your site requests if you're using a subdomain or a completely different domain. 1.- 如果您使用子域或完全不同的域,请检查服务器的CORS 策略是否对您的站点请求开放。 This depends on your service type (mine was ASP.NET MVC).这取决于您的服务类型(我的是 ASP.NET MVC)。

2.- I had a conflict with my Web App and my Web API (since I didn't use ASP.NET Core from the very first time), so I couldn't use my fetch function normally, but if is not your case, try to use the fetch API from your client as suggested here because it can expect the blob type directly from the request (you can also try to use the Axios or the XMLHttpRequest approach). 2.- I had a conflict with my Web App and my Web API (since I didn't use ASP.NET Core from the very first time), so I couldn't use my fetch function normally, but if is not your case, try使用这里建议的从您的客户端fetch API因为它可以直接从请求中获取 blob 类型(您也可以尝试使用AxiosXMLHttpRequest方法)。

3.- (MOST IMPORTANT) What finally worked for me was to convert to Base64 the file from the server BEFORE sending the response file and then decode it from the client with a function. 3.-(最重要)最终对我有用的是在发送响应文件之前将来自服务器的文件转换为 Base64,然后使用 function从客户端对其进行解码。 Skipping this step only threw a blank PDF even when I could preview it from the Safari inspector and had a perfectly PDF response with all its bytes, but this wasn't enough for the browser to download it correctly;即使我可以从 Safari 检查器预览它并且有一个完美的 PDF 响应及其所有字节,跳过这一步只会抛出一个空白 PDF ;但这还不足以让浏览器正确下载它so try to encode it first.所以首先尝试对其进行编码。

4.- Finally remember to define the Data type and Content type that best suits your needs in your request. 4.- 最后记得在您的请求中定义最适合您需求的数据类型和内容类型。 In my case:就我而言:

...
dataType: 'text',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
success: function(response) {
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM