Struts 2 Download - How to configure the file name dynamically?
Asked Answered
E

2

9

I am developing one application , where people will download the required file from a location mentioned in the DB to their Local. I am using struts 2 for downloading the file from the server . I can download the file without any exception and it works perfectly. But the files am download has the filename i specified in struts.xml , i want it to be the exact filename which am downloading . example if the original file name is struts.pdf , i am downloading it as download.pdf, how to prevent it and download the file with actual filename

My struts.xml configuration as follows ,

<action name="download" class="action.DownloadAction">
        <result name="success" type="stream">
            <param name="contentType">application/octet-stream</param>
            <param name="inputName">fileInputStream</param>
            <param name="contentDisposition">attachment;filename="download.log"</param>
            <param name="bufferSize">1024</param>
        </result>
        <result name="error">/live/useradminerror.jsp</result>
    </action> 

And i forgot to mention am using struts2-jquery for developing the UI .Please help me in this , as am in very critical stage of my project .

Edirne answered 1/8, 2012 at 4:49 Comment(0)
N
15

IF i am correct you want to pass the file which is being stored in your DB, if this is the case you can easily do this by passing all those parameters from you action class like

class MyFileDownloadAction extends ActionSupport{

     private String fileName;
     // getter and setter

    public String fileDownload() throws exception{
      // file download logic
      fileName ="abc"  // can set name dynamic from DB
   }

}

<action name="download" class="action.DownloadAction">
        <result name="success" type="stream">
            <param name="contentType">application/octet-stream</param>
            <param name="inputName">fileInputStream</param>
            <param name="contentDisposition">attachment;filename="${filename}"</param>
            <param name="bufferSize">1024</param>
        </result>
        <result name="error">/live/useradminerror.jsp</result>
    </action> 

You can pass each parameter dynamically in your struts.xml class.Hope this will help you This is how you will use this file name in your XML

Novah answered 1/8, 2012 at 4:56 Comment(5)
Thank you for the response , will it download exactly the same name as in database.Edirne
yes it will, since in this case you are passing file name at run time from your action classNovah
Thank you Umesh , i can download the file with actual filenameEdirne
just a tip: contentDisposition is not working in chrom and opera use Content-Disposition too!Succubus
TY. Just a comment, it is not possible to call a custom function, it is always needed to declare a private field and add the logic in the getter of the field.Hawkes
P
3

For annotations in struts, its same. The solution was very helpful. Thank you. The "contentType" for me did not make much difference.

@Action(value = "/download", results = { @Result(name = "success", type = "stream", 
params= {"contentType", "application/octet-stream", "inputName","fileInputStream",    
"contentDisposition","attachment; filename=\"${fileName}\"", "bufferSize", "1024" })
})
Poussin answered 24/6, 2013 at 8:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.