Pradnya
Pradnya

Reputation: 669

Downloaded attachment doesn't show correct extension name

I am using postgresql as backend. I have saved attachments & attachment title in case_attachments table. When I click to download attachments, it does download it. But the attachment name comes as this downloadAttachFile and the type as action . I would be saving attachments of multiple types such as jpg, pdf, doc etc. So while downloading it is important that the filename & extension is correct.

This is my struts.xml

        <action name="downloadAttachFile" class="abc.actions.mypackage" method="downloadAttachFile">
        <result name="success" type="stream">
        <param name="contentType">application/octet-stream/text/plain/doc</param>
        <param name="inputName">fileInputStream</param>
        <param name="contentDisposition">attachment;filename=%{AttachFileName}</param>
        <param name="bufferSize">1024</param>           
        </result>               
    </action> 

And action method

    public String downloadAttachFile() throws FileNotFoundException {
    String AttachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
    fileInputStream = new FileInputStream(new File(AttachFileName));
    return SUCCESS;
}

When I right click and open it using the correct tool it opens correctly. So only problem here is the name & extension type.

Upvotes: 0

Views: 66

Answers (1)

Andrea Ligios
Andrea Ligios

Reputation: 50203

It is not clear why, if you are retrieving it from the database, you are reading it from the request... btw:

  1. Your action property must be private, at class level (and not declared at method level as you did), with a getter, and starting with a lowercase character to follow conventions:

    private String attachFileName;
    
    public String getAttachFileName(){
        return attachFileName;
    }
    
    public String downloadAttachFile() throws FileNotFoundException {
        attachFileName = ServletActionContext.getRequest().getParameter("myFileFileName");
        fileInputStream = new FileInputStream(new File(AttachFileName));
        return SUCCESS;
    }
    
  2. You need to store, retrieve and set the real, correct contentType. This:

    <param name="contentType">application/octet-stream/text/plain/doc</param>
    

    is a monster that may turn your browser into a nightmare generator.

Upvotes: 1

Related Questions