使用ajax下载生成的excel文件

时间:2022-06-21 01:22:58

I am developing an application using struts2 framework. When user provides date range, I need to prepare the reports excel file and show that in download pop-up. I am able to create io stream of excel, but I am not getting how can I open that pop-up using ajax.

我正在使用struts2框架开发一个应用程序。当用户提供日期范围时,我需要准备报告excel文件并在下载弹出窗口中显示。我能够创建exo的io流,但是我没有得到如何使用ajax打开弹出窗口。

I am generating file on the fly and returns the stream. Therefore not able to give file path. I want ajax call to show some message like "Please wait..". Because file generation takes more time and it looks like link is not working..

我正在生成文件并返回流。因此无法提供文件路径。我想要ajax调用来显示一些消息,比如“Please wait ..”。因为文件生成需要更多时间,看起来链接不起作用..

3 个解决方案

#1


1  

There's nothing Ajax about downloading a file.

下载文件没有Ajax。

Your request can be an ajax request, but simply give the generated file path and the browser will do the downloading.

您的请求可以是ajax请求,但只需提供生成的文件路径,浏览器就会进行下载。

#2


1  

No need to perform any AJAX calls.

无需执行任何AJAX调用。

You just need to return the byte[] as a Struts2 Stream result, configured with a ContentDisposition: attachment; (and not ContentDisposition: inline, that will try to open it inside the browser instead of asking about downloading or opening with a desktop application).

您只需要将byte []作为Struts2 Stream结果返回,并使用ContentDisposition:attachment配置; (而不是ContentDisposition:inline,它会尝试在浏览器中打开它,而不是询问下载或打开桌面应用程序)。

To notify the user that something is going on, that the request is sent and the system is not frozen, you need an Loading OVERLAY.

要通知用户发生了某些事情,发送请求并且系统未冻结,您需要加载OVERLAY。

An Overlay is an element that is placed over your page, generally with a partially transparent background, an animated image saying "Loading..." , and a modal behavior (it won't close until the page is changed, and it will prevent double post of the same request, like double clicks etc...).

Overlay是放置在页面上的元素,通常具有部分透明背景,动画图像“Loading ...”和模态行为(在页面更改之前不会关闭,它会阻止同一请求的双重帖子,如双击等...)。

Of course if you open an attachment, the page will not change, then you'll need to intercept the end of the downloading and close the overlay by yourself, or give the user a button to close it.

当然,如果你打开一个附件,页面不会改变,那么你需要截取下载的结束并自己关闭叠加,或者给用户一个关闭它的按钮。

To intercept it, you can try the Struts2 Execute and Wait Interceptor.

要拦截它,您可以尝试Struts2执行和等待拦截器。

If you want to create a custom overlay, you can generate your Loading images with the ajaxload.info Generator.

如果要创建自定义叠加层,可以使用ajaxload.info生成器生成加载图像。

If you instead don't want to reinvent the wheel, you can take a look at existing overlays, like those from jQuery TOOLS.

如果您不想重新发明*,可以查看现有的叠加层,例如来自jQuery TOOLS的叠加层。

#3


0  

On the success method of ajax do not return the stream, instead save the file on server and open a pop up with address to an action that returns correct file. But pop ups might be blocked in some browsers so you need to allow that for your site (local host or other URL)

在ajax的成功方法上,不返回流,而是将文件保存在服务器上,并打开一个弹出的地址,返回正确的文件。但是在某些浏览器中可能会阻止弹出窗口,因此您需要为您的站点(本地主机或其他URL)允许弹出窗口

#1


1  

There's nothing Ajax about downloading a file.

下载文件没有Ajax。

Your request can be an ajax request, but simply give the generated file path and the browser will do the downloading.

您的请求可以是ajax请求,但只需提供生成的文件路径,浏览器就会进行下载。

#2


1  

No need to perform any AJAX calls.

无需执行任何AJAX调用。

You just need to return the byte[] as a Struts2 Stream result, configured with a ContentDisposition: attachment; (and not ContentDisposition: inline, that will try to open it inside the browser instead of asking about downloading or opening with a desktop application).

您只需要将byte []作为Struts2 Stream结果返回,并使用ContentDisposition:attachment配置; (而不是ContentDisposition:inline,它会尝试在浏览器中打开它,而不是询问下载或打开桌面应用程序)。

To notify the user that something is going on, that the request is sent and the system is not frozen, you need an Loading OVERLAY.

要通知用户发生了某些事情,发送请求并且系统未冻结,您需要加载OVERLAY。

An Overlay is an element that is placed over your page, generally with a partially transparent background, an animated image saying "Loading..." , and a modal behavior (it won't close until the page is changed, and it will prevent double post of the same request, like double clicks etc...).

Overlay是放置在页面上的元素,通常具有部分透明背景,动画图像“Loading ...”和模态行为(在页面更改之前不会关闭,它会阻止同一请求的双重帖子,如双击等...)。

Of course if you open an attachment, the page will not change, then you'll need to intercept the end of the downloading and close the overlay by yourself, or give the user a button to close it.

当然,如果你打开一个附件,页面不会改变,那么你需要截取下载的结束并自己关闭叠加,或者给用户一个关闭它的按钮。

To intercept it, you can try the Struts2 Execute and Wait Interceptor.

要拦截它,您可以尝试Struts2执行和等待拦截器。

If you want to create a custom overlay, you can generate your Loading images with the ajaxload.info Generator.

如果要创建自定义叠加层,可以使用ajaxload.info生成器生成加载图像。

If you instead don't want to reinvent the wheel, you can take a look at existing overlays, like those from jQuery TOOLS.

如果您不想重新发明*,可以查看现有的叠加层,例如来自jQuery TOOLS的叠加层。

#3


0  

On the success method of ajax do not return the stream, instead save the file on server and open a pop up with address to an action that returns correct file. But pop ups might be blocked in some browsers so you need to allow that for your site (local host or other URL)

在ajax的成功方法上,不返回流,而是将文件保存在服务器上,并打开一个弹出的地址,返回正确的文件。但是在某些浏览器中可能会阻止弹出窗口,因此您需要为您的站点(本地主机或其他URL)允许弹出窗口