File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes Force a file download in HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Force a file download in HTML" Watch "Force a file download in HTML" New topic

Force a file download in HTML

Chethan Kumar Baliga

Joined: Sep 17, 2002
Posts: 11
I am looking for a file download feature.
I have implemented the same using "href"
Ex: -
<a href="http://servername ort/folder/file.txt"> Download text file</a>
On click of this link however, the file directly opens in the browser window instead of being downloaded. Right-clicking on the link and selecting "Save Target As" is one option to download it.
What I am looking for is, ceratin file types (viz. zip, tif etc.) promptly show the "File Download" dialog box, others (viz. txt, html etc.) directly open in the browser window.
This is because of the "Content-type". There is some tag (meta or something else) where one can specify the "Content-type" OR "Content-disposition" so that the user is forced to download the file, no matter what (File Download should appear!!!). Does anyone have the exact code for this in simple HTML???
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I'm wondering if the 'a href' supports the 'type' attribute that exists in other tags...
<a href="..." type="application/unknown">... etc
Might be worth a try.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

if David's suggestion doesnt work, i would just compress(zip) any file you want to send rather than display

Visit my download page
I agree. Here's the link:
subject: Force a file download in HTML
It's not a secret anymore!