如何在jquery ajax调用后将文件输出到浏览器

时间:2022-10-08 13:03:34

I have a link on the website to a php file that generates native excel file on a fly ant outputs it directly to browser via headers for user to to open/save. Since it takes some time for the file to be generated I'd like to use jQuery Ajax to make the call and use some loading animation in the mean while.

我在网站上有一个链接到一个php文件,它可以在飞行中生成本机excel文件,通过标题将其直接输出到浏览器,供用户打开/保存。由于生成文件需要一些时间,因此我想使用jQuery Ajax进行调用并使用一些加载动画。

The only thing I'm not sure how to do is how to output the file into the browser after Ajax call? Is it even possible?

我唯一不确定如何做的是在Ajax调用后如何将文件输出到浏览器中?它甚至可能吗?

3 个解决方案

#1


3  

(N.B. This is a paraphrasing of @dmitry's answer, but just elaborated upon)

(N.B.这是对@dmitry的答案的解释,但刚刚详细阐述)

The problem you have is that there is no means of directly returning a file to the user via AJAX - the browser has to request the file using a normal, synchronous HTTP request.

您遇到的问题是无法通过AJAX直接将文件返回给用户 - 浏览器必须使用正常的同步HTTP请求来请求文件。

To solve this, your PHP will need to:

要解决这个问题,您的PHP需要:

  1. Generate the Excel file as normal.
  2. 正常生成Excel文件。
  3. Instead of writing the file back to the user, save it somewhere on the server's filesystem (i.e. using file_put_contents() or similar).
  4. 而不是将文件写回用户,而是将其保存在服务器的文件系统上(即使用file_put_contents()或类似文件)。
  5. Return the file path to the user.
  6. 将文件路径返回给用户。

Your JS, on receiving this response will then need to:

您的JS在收到此响应后将需要:

  1. Read the Excel file path back from the PHP script.
  2. 从PHP脚本中读取Excel文件路径。
  3. Open the Excel file in a new tab/window using window.open() (or redirect in the current tab/window by setting location.href).
  4. 使用window.open()在新选项卡/窗口中打开Excel文件(或通过设置location.href在当前选项卡/窗口中重定向)。

#2


1  

I think you can make a trick: generate file and return path to generated file in your Ajax response and then just call

我想你可以制作一个技巧:在Ajax响应中生成文件并返回生成文件的路径,然后调用

window.location = fileUrl

also there some techniques with iframe Ajax

还有iframe Ajax的一些技巧

#3


0  

Why don't you use iframe where you load the actual php file that generates the excel file? That way you won't block the UI while the file is being generated and browser will trigger download dialog.

你为什么不使用iframe加载生成excel文件的实际php文件?这样,在生成文件时,您将不会阻止UI,浏览器将触发下载对话框。

There's no way that you can send a file via javascript, at least not in the manner of forcing the browser to open the download dialog.

您无法通过javascript发送文件,至少不会强制浏览器打开下载对话框。

#1


3  

(N.B. This is a paraphrasing of @dmitry's answer, but just elaborated upon)

(N.B.这是对@dmitry的答案的解释,但刚刚详细阐述)

The problem you have is that there is no means of directly returning a file to the user via AJAX - the browser has to request the file using a normal, synchronous HTTP request.

您遇到的问题是无法通过AJAX直接将文件返回给用户 - 浏览器必须使用正常的同步HTTP请求来请求文件。

To solve this, your PHP will need to:

要解决这个问题,您的PHP需要:

  1. Generate the Excel file as normal.
  2. 正常生成Excel文件。
  3. Instead of writing the file back to the user, save it somewhere on the server's filesystem (i.e. using file_put_contents() or similar).
  4. 而不是将文件写回用户,而是将其保存在服务器的文件系统上(即使用file_put_contents()或类似文件)。
  5. Return the file path to the user.
  6. 将文件路径返回给用户。

Your JS, on receiving this response will then need to:

您的JS在收到此响应后将需要:

  1. Read the Excel file path back from the PHP script.
  2. 从PHP脚本中读取Excel文件路径。
  3. Open the Excel file in a new tab/window using window.open() (or redirect in the current tab/window by setting location.href).
  4. 使用window.open()在新选项卡/窗口中打开Excel文件(或通过设置location.href在当前选项卡/窗口中重定向)。

#2


1  

I think you can make a trick: generate file and return path to generated file in your Ajax response and then just call

我想你可以制作一个技巧:在Ajax响应中生成文件并返回生成文件的路径,然后调用

window.location = fileUrl

also there some techniques with iframe Ajax

还有iframe Ajax的一些技巧

#3


0  

Why don't you use iframe where you load the actual php file that generates the excel file? That way you won't block the UI while the file is being generated and browser will trigger download dialog.

你为什么不使用iframe加载生成excel文件的实际php文件?这样,在生成文件时,您将不会阻止UI,浏览器将触发下载对话框。

There's no way that you can send a file via javascript, at least not in the manner of forcing the browser to open the download dialog.

您无法通过javascript发送文件,至少不会强制浏览器打开下载对话框。