JQuery Ajax 调用 PDF 文件下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7578124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:00:51  来源:igfitidea点击:

JQuery Ajax call for PDF file download

asp.net-mvc-3jquery

提问by Nikshep

My question is similar to Download and open pdf file using Ajax

我的问题类似于使用 Ajax 下载并打开 pdf 文件

But not exactly the same , the reason I want an JQuery ajax is that my file is being generated dynamically from the data which would be fetched from the same page.

但不完全相同,我想要一个 JQuery ajax 的原因是我的文件是从从同一页面获取的数据动态生成的。

So basically I have a Json Object which needs to be sent to the server which would dynamically generate a file and send it back to the browser.

所以基本上我有一个 Json 对象,它需要发送到服务器,它会动态生成一个文件并将其发送回浏览器。

I would not like to use query strings on an anchor with my Json object stringyfied , as I think it would be a potential threat as query strings have character restrictions ( am I right here ?).

我不想在带有我的 Json 对象 stringyfied 的锚点上使用查询字符串,因为我认为这将是一个潜在的威胁,因为查询字符串有字符限制(我在这里吗?)。

Please let me know If my workflow is right or I can achieve the same thing thing using a different flow.

如果我的工作流程是正确的,或者我可以使用不同的流程实现同样的事情,请告诉我。

采纳答案by Darin Dimitrov

You should not use AJAX to download files. This doesn't work. This being said you are left with 2 options:

您不应该使用 AJAX 下载文件。这不起作用。话虽如此,您还有两个选择:

  1. action link and query string parameters or if the download must be trigerred at some specific javascript event you could also set the window.location.hrefto the action that is supposed to generate the PDF file and pass query string parameters to it.

  2. or if you are afraid that you have lots of data to pass to the controller action that will generate the PDF to download you use a <form>with method="POST"and inside you use hidden fields to store as much parameters and data as you like:

    @using (Html.BeginForm("download", "home"))
    {
        ... hidden fields containing values that need to be sent to the server
        <input type="submit" value="Download PDF" />
    }
    
  1. 操作链接和查询字符串参数,或者如果下载必须在某些特定的 javascript 事件中触发,您还可以将 设置window.location.href为应该生成 PDF 文件并将查询字符串参数传递给它的操作。

  2. 或者,如果您担心有大量数据要传递给将生成 PDF 下载的控制器操作,您可以使用<form>withmethod="POST"和 inside 您使用隐藏字段来存储尽可能多的参数和数据:

    @using (Html.BeginForm("download", "home"))
    {
        ... hidden fields containing values that need to be sent to the server
        <input type="submit" value="Download PDF" />
    }
    

回答by Hitesh

Instead of making one more ajax call in your page you can use anchor tag and php force download to perform pdf download

您可以使用锚标记和 php 强制下载来执行 pdf 下载,而不是在您的页面中再进行一次 ajax 调用

HTML
    <a href="www.example.com/download_file.php?file_source=example.pdf">Download pdf here</a>

PHP
<?php
$fullPath = $_GET['fileSource'];
if($fullPath) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        header("Content-type: application/pdf"); // add here more headers for diff. extensions
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    if($fsize) {//checking if file size exist
      header("Content-length: $fsize");
    }
    readfile($fullPath);
    exit;
}
?>

I am checking for file size because if you load pdf from CDN cloudfront, you won`t get the size of document which forces the document to download in 0kb, To avoid this i am checking with this condition

我正在检查文件大小,因为如果您从 CDN cloudfront 加载 pdf,您将无法获得强制文档以 0kb 下载的文档大小,为了避免这种情况,我正在检查这种情况

  if($fsize) {//checking if file size exist
      header("Content-length: $fsize");
    }

I too was struggling with this problem, above code worked for me I hope it helps

我也在为这个问题而苦苦挣扎,上面的代码对我有用,希望它有所帮助

回答by RohannG

I have a very sure answer. You can't download PDF file while using the ajax request to server. So instead of that you should use html actionlink. for example

我有一个非常肯定的答案。使用 ajax 请求服务器时无法下载 PDF 文件。因此,您应该使用 html actionlink。例如

@Html.ActionLink("Convert Data into PDF", "PDFView", null, new { @class= "btn btn-info" });

Above code will generate a link button and you can access Action Method of controller. Also you can use any technique to return PDF file from controller for example you can return PDF file using FileResultclass.

上面的代码将生成一个链接按钮,您可以访问控制器的 Action 方法。您也可以使用任何技术从控制器返回 PDF 文件,例如您可以使用FileResult类返回 PDF 文件。