wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to download a file in IE? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to download a file in IE?" Watch "How to download a file in IE?" New topic
Author

How to download a file in IE?

Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Something what should be so basic seems like it is going to require a more technical solution. By default IE opens up certain documents (ie Word docs) instead of downloading them when clicking on a link to a file.

Is there a way around this in Java that works for all versions of IE? I have read around and found lots of information on PHP but there still seems to be a lot of issues with the different versions of IE.

I am looking for a few pointers on how to accomplish download when clicking on a link to a file (or /download?filename.extention which would map to a Servlet). Ideally the solution would be work across all versions of IE, but with knowing how buggy IE is I think I may be dreaming there.

Anyone had any experience with doing this before?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Why do you think that it is appropriate to try and circumvent the way that the user has set up his or her browser?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
Because it interrupts my design..? :-p

But seriously, the reason why I am wanting to do this is because all of the Word documents I am wanting to download require a user to be logged in to download them. As such, when opening a file direct into Word (instead of downloading) then the file doesn't open since Word isn't authorised to download the file.

I am looking to turn the Word documents into HTML pages in the long term, but this fix would be ideal for the short term.

Thanks
Michael
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Not sure I'm getting that -- but in any case, if you've found a PHP solution that does what you want, I don't see why you cant replicate it in Java. After all, they both just return an HTTP response.
Michael Cropper
Ranch Hand

Joined: Sep 30, 2009
Posts: 137
The bit you didn't understand - basically, the .doc files are located in a /secure/ folder on the site, which requires a user to be logged in to view. So when Word opens up the .doc file (instead of downloading) it must be acting as a semi-web-browser (or something) which means that it is trying to access the file from Word (which isn't logged in to the site) opposed to accessing the file through the web browser and saving to a temp location, then opening from there. So when IE opens the .doc file in Word, the information that is displayed is the login screen, since the website is set up to automatically redirect users there when they are not logged in.

Does that explain a little more?

I will have a go at replicating the various PHP scripts I have found and see what I can do. The scripts that I found were still listing a lot of issues with how IE handles different header responses and not behaving correctly which is why I was seeing if anyone else has tried / completed something like this before. (saves me making the same mistakes).

So whilst I am having a go at fixing this, if there are any tips on how to overcome the IE issues surrounding this it would certainly be useful. :-)

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

It sounds like your server isn't returning the correct content type. It shouldn't be sending a response with content type which says the data is Word (whatever that content type code is) unless it's actually going to be returning Word data. If it returns such a content type, and then forwards to HTML because the user isn't logged in, then yeah, the browser and its minions are going to assume they're getting Word data.

Here's how it should work: the browser checks to see if the user is logged in. If not, it forwards to the login page and returns the text/html content type. Otherwise it returns the MS Word content type and the MS Word data.
he Mr
Greenhorn

Joined: Nov 03, 2011
Posts: 1
I think you can create a Zip File
not a doc File
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to download a file in IE?