JQuery Ajax调用PDF文件下载

时间:2021-07-17 09:45:19

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.

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

3 个解决方案

#1


8  

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.href to the action that is supposed to generate the PDF file and pass query string parameters to it.

    动作链接和查询字符串参数或者如果必须在某些特定的javascript事件中进行下载,您还可以将window.location.href设置为应该生成PDF文件并将查询字符串参数传递给它的操作。

  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:

    或者,如果您害怕将大量数据传递给将生成PDF下载的控制器操作,请使用带有method =“POST”的

    ,并在其中使用隐藏字段存储尽可能多的参数和数据你喜欢:

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

#2


1  

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

我也在努力解决这个问题,上面的代码对我有用,我希望它有所帮助

#3


0  

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 FileResult class.

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

#1


8  

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.href to the action that is supposed to generate the PDF file and pass query string parameters to it.

    动作链接和查询字符串参数或者如果必须在某些特定的javascript事件中进行下载,您还可以将window.location.href设置为应该生成PDF文件并将查询字符串参数传递给它的操作。

  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:

    或者,如果您害怕将大量数据传递给将生成PDF下载的控制器操作,请使用带有method =“POST”的

    ,并在其中使用隐藏字段存储尽可能多的参数和数据你喜欢:

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

#2


1  

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

我也在努力解决这个问题,上面的代码对我有用,我希望它有所帮助

#3


0  

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 FileResult class.

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