简体   繁体   中英

How to make files download instead of opening in browser?

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.

Here is one way of doing it:

  1. Create a web Filter (or this way )
  2. Map this filter to the PDF URL.
  3. 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.org/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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM