文件在Struts中下载start事件 [英] File Download start event in Struts

查看:82
本文介绍了文件在Struts中下载start事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的struts应用程序中,用户可以从服务器下载文件。

In my struts application, a user can download a file from the server.

我希望在按钮单击之间显示一个微调器(启动下载)和文件准备下载。是否有文件开始下载时触发​​的事件?我假设这将是某种页面加载事件。

I want to show a spinner during the time between the button click (to initiate the download) and file is ready to download. Is there an event which is triggered when the file starts to download? I assume it would be some sort of page load event.

这是我的struts xml的部分:

This is the section from my struts xml:

<action name="getFile" method="getFile" class="foo.pack.TAction">
    <result name="success" type="stream">
        <param name="contentType">application/pdf</param>
        <param name="contentDisposition">attachment;filename=${fileName}</param>
    </result>
    <result name="login" type="redirect">/login</result>
    <result name="error" type="tiles">showError</result>
</action>

点击按钮,我设置 window.location = localhost:8080 / getFile .action
下一步下载文件(n秒后)

On button click, I set window.location = localhost:8080/getFile.action The file is downloaded next (after n seconds)

在这段时间内显示微调的方法是什么对于该文件,从服务器获取文件?

What would be a way to show the spinner during the time for which the file is being fetched from the server?

推荐答案

对于这个答案,我将假设plainJSP / Servlet / HTML / JS,因为我不使用Struts。对于一个先进的Struts(和jQuery)用户,它应该是微不足道的,将其移植到Struts(和jQuery)。

For this answer, I'll assume "plain" JSP/Servlet/HTML/JS as I don't use Struts. For an advanced Struts (and jQuery) user it should however be trivial enough to port it to Struts (and jQuery).

为此,您可以设置一个cookie下载请求的响应,并有JavaScript来轮询该cookie。一旦下载准备好投放,该cookie将可用JavaScript。为确保在同一会话中的各种浏览器窗口/标签工作,最好是生成唯一的下载令牌,并将其作为请求参数传回,以便服务器端将其设置为Cookie值。不要忘记以后过期cookie以防止cookie污染。

To the point, you could set a cookie on the response of the download request and have JavaScript to poll for that cookie. Once the download is ready to be served, the cookie will be available in JavaScript. To ensure working across various browser windows/tabs in the same session, best is to generate an unique download token and pass it back as request parameter so that the server side can set it as a cookie value. Don't forget to expire the cookie afterwards to prevent cookie pollution.

基本上(您可以替换< span> < img> 指向一些微调GIF):

Basically (you could substitute the <span> with an <img> pointing to some spinner gif):

<input type="button" value="Download" onclick="download()" />
<span id="wait" style="display:none">Please wait while we prepare the download...</span>

使用这个JavaScript(使用jQuery时, jquery-cookie plugin 可能会有帮助):

With this JavaScript (when using jQuery, the jquery-cookie plugin may be helpful):

function download() {
    var token = new Date().getTime();
    var wait = document.getElementById("wait");
    wait.style.display = "block";

    var pollDownload = setInterval(function() {
        if (document.cookie.indexOf("download=" + token) > -1) {
            document.cookie = "download=" + token + "; expires=" + new Date(0).toGMTString() + "; path=/";
            wait.style.display = "none";
            clearInterval(pollDownload);
        }
    }, 500);

    window.location = "download?token=" + token;
}

在servlet(或Struts操作(如果适用))中:

And in the servlet (or Struts action if applicable):

// Prepare download here.
// ...

// Once finished, set cookie and stream download to response.
Cookie cookie = new Cookie("download", request.getParameter("token"));
cookie.setPath("/");
response.addCookie(cookie);
// ...

这篇关于文件在Struts中下载start事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆