How to set a progress bar when downloading a file using struts2 and Ajax

I can’t put a progress bar because it directly redirects the page and the file loads.

+2
source share
1 answer

So many questions (most of them implicitly) in one!

How to set a progress bar when downloading a file using struts2 and Ajax

  • Do not use the AJAX download if it is not needed. When you open the file in a browser ( contentDisposition: inline), just use the new tab (/ Window). When you upload a file ( contentDisposition: attachment), the current page will not be affected. You can find several ways to do this in this answer , for example:

    <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 the browser progress bar?

  1. , :

    enter image description here

    , . , contentLength, 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;
    }
    

, . , ,

  1. , -. :

    , , , , . , , :

    // 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;
        }
    }
    

    .

+2

All Articles