gpt4 book ai didi

java - 使用struts2和Ajax下载文件时如何放置进度条

转载 作者:行者123 更新时间:2023-11-30 08:17:04 30 4
gpt4 key购买 nike

我无法放置进度条,因为它直接重定向页面并下载文件。

最佳答案

这么多问题(其中大部分是隐含的)在一个问题中!

How to put progress bar when downloading file using struts2 and Ajax

  1. 如果不需要,请不要使用 AJAX 下载。当您在浏览器中打开文件时 (contentDisposition: inline),只需使用一个新的 Tab(/Window)。下载文件 (contentDisposition: attachment) 时,当前页面不会受到影响。您可以在 this answer 中找到执行此操作的几种方法。 ,例如:

    <s:url action="downloadAction.action" var="url">
    <s:param name="param1">value1</s:param>
    </s:url>
    <s:a href="%{url}" >download</s:a>

how can we put browser progress bar?

  1. 每个浏览器都有一个在下载文件时显示的内置进度条:

    enter image description here

    只有在未提供下载文件长度的情况下,浏览器才能绘制进度条。要指示浏览器,您可以使用 contentLength header ,它也可以直接在 Stream 中使用。结果:

    <result name="success" type="stream">    
    <param name="contentType">image/jpeg</param>
    <param name="contentDisposition">attachment;filename="document.pdf"</param>
    <param name="contentLength">${lengthOfMyFile}</param>
    </result>
    private long lengthOfMyFile; // with Getter

    public String execute(){
    /* file loading and stuff ... */
    lengthOfMyFile = myFile.length();
    return SUCCESS;
    }

Suppose if file is too heavy. So it take time so I want to prevent user not click to other button

  1. 如果您想节省带宽,那么您需要处理您的Web 服务器 配置。这篇文章可能会有所帮助:

    相反,如果您不关心防止泛洪请求,而只是防止客户端的多个并发下载,您可以使用 session 变量,将其放在开头并在末尾删除方法,在下载操作开始时检查它是否存在。如果存在,则不会下载,否则,您将:

    // The Action must implement the SessionAware interface

    private Map<String,Object> session; // with Setter
    private final static String BUSY = "I'm busy. Try again";

    public String execute(){
    if (session.get(BUSY)!=null){
    LOG.debug("Another download is in progress. I stop here");
    return NONE;
    }
    try {
    session.put(BUSY,BUSY);
    /* file loading and stuff ... */
    } finally {
    session.remove(BUSY);
    return SUCCESS;
    }
    }

    古老的信号量。

关于java - 使用struts2和Ajax下载文件时如何放置进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28061516/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com