I am a new bie, i want files to get downloaded when user clicks on download option its opening in the browser instead of download option like save as/open.Here i referred for the same and every where they have suggested to use
Response.AddHeader("Content-disposition", "attachment; filename=" + Name);
But i don't know where and how to use. Actually i'm getting the url value from query written which return url as one of the object of bean stored in arraylist(This list is having other values also with url ). I am having the url values inside the arraylist as bean as like
type=.pdf
release date=12/3/08
name=hai.pdf
url=/files/en/soft/doc/docs/hai.pdf
I am getting this array list in my controller like this
ArrayList details = dao.getdetails(Bean.getNumber());
and pass this into view like this
Map.put("details", details);
modelView.setViewName("details_list");
modelView.addAllObjects(Map);
return modelView;
in jsp i have iterated this array list and diplays the content like this
Type name Release Date
.txt hai.pdf May 21st 2012 Download
.txt hello.txt May 21st 2012 Download
For download i have used like this in jsp
<td colspan="2" valign="top">
<a href="${details.Url}"/>
<img src="/images/download.gif" alt="Download" border="0" align="right"></a>
</td>
here on click of download its opening in browser.I need this to be downloaded instead. Please help me in how to use or handle
response.setHeader("Content-Disposition", "attachment;");
where to add the above for my requirement or if i can do with any java script also.Please help me in solving the above.
I am a new bie, i want files to get downloaded when user clicks on download option its opening in the browser instead of download option like save as/open.Here i referred for the same and every where they have suggested to use
Response.AddHeader("Content-disposition", "attachment; filename=" + Name);
But i don't know where and how to use. Actually i'm getting the url value from query written which return url as one of the object of bean stored in arraylist(This list is having other values also with url ). I am having the url values inside the arraylist as bean as like
type=.pdf
release date=12/3/08
name=hai.pdf
url=/files/en/soft/doc/docs/hai.pdf
I am getting this array list in my controller like this
ArrayList details = dao.getdetails(Bean.getNumber());
and pass this into view like this
Map.put("details", details);
modelView.setViewName("details_list");
modelView.addAllObjects(Map);
return modelView;
in jsp i have iterated this array list and diplays the content like this
Type name Release Date
.txt hai.pdf May 21st 2012 Download
.txt hello.txt May 21st 2012 Download
For download i have used like this in jsp
<td colspan="2" valign="top">
<a href="${details.Url}"/>
<img src="/images/download.gif" alt="Download" border="0" align="right"></a>
</td>
here on click of download its opening in browser.I need this to be downloaded instead. Please help me in how to use or handle
response.setHeader("Content-Disposition", "attachment;");
where to add the above for my requirement or if i can do with any java script also.Please help me in solving the above.
Share Improve this question asked Jun 11, 2012 at 5:10 antoanto 3253 gold badges10 silver badges18 bronze badges 6- @Japs :Can you please Help me – anto Commented Jun 11, 2012 at 5:45
- Oh my god, I have seen this question many times already. – Derek 朕會功夫 Commented Jun 11, 2012 at 5:49
- @Derek: yes but how to implement according to my requirement – anto Commented Jun 11, 2012 at 5:50
- What language are you using there? Java? JavaScript? PHP? – Derek 朕會功夫 Commented Jun 11, 2012 at 5:53
- @Derek:java spring-mvc, i have tagged the same. please refer my post.If can use javascript also suggest me the same of how to implement. – anto Commented Jun 11, 2012 at 5:55
3 Answers
Reset to default 5Here is one way of doing it:
- Create a web Filter (or this way )
- Map this filter to the PDF URL.
- In the
doFilter()
method, set the response header for content download.
Example :
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String name = request.getAttribute("filename");
response.addHeader("Content-disposition", "attachment; filename=" + name);
chain.doFilter(request, response);
}
You can set the filename as an request attribute (reqest.setAttribute()) from your controller class
Filters are pretty standard in Java web stack.
It's depend on the header which browser get in response.
Suppose if header is image/png then browser will show it. Same way if you send same image with application/octet-stream then browser will force to download it.
take a look http://en.wikipedia/wiki/Byte_stream
In a project I have figure out that sending request from browser are different.
If you upload a image from Firefox or IE then it's will upload them as image/png wherever chrome upload them as application/octet-stream.
just try to add header
response.setHeader("Content-Type: application/force-download");