File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes File downloads... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File downloads..." Watch "File downloads..." New topic
Author

File downloads...

Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
I've implemented a page where users can upload files, and then others can download them (or view them).
I've just listed links to the locations of all the files. Normally the browser asks if you wish to download it or if its something the browser understands (pdf, doc, gif etc). Everything works fine except excel files, firefox will ask if you wish to open it with excel or download it. IE will interpret it as a text file and list a bunch of "random text", if you right click the link IE will offer to save the file as .htm.

Why is this? I've visited other sites where excel files are opened seemlessly by my browser. I've checked the uploaded excel files they open under excel fine. They weren't corrupted during the upload and I've "re-downloaded" them and excel will again open them with out a problem. Just the browser won't recognize it as an excel file.

-Tad
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
You need to set the content type of your response to "application/excel". You should be able to accomplish this by doing something similar to the following:

response.setContentType("application/excel");
[ March 31, 2005: Message edited by: Jason Menard ]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Check out this link for more info:

http://www.javaworld.com/javaworld/javatips/jw-javatip94.html
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
I guess I'm being (or was being) naive. I merely placed a link to the file's location (almost as if it were an html document).

Thanks for the link.

-Tad
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
Is there a mime/type I could set all files to so that the file would download to the users machine instead of opening in the browser regardless of extension/file type? So that even an .htm file would instead be downloaded?

-Tad
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
It really depends on the client's browser preferences. However, you might try application/octet-stream, which is the MIME type normally associated with .exe and .bin files. As a general practice though, it's best not to try and override the client's preferences. With this in mind, it's preferable to send the correct MIME type for the data you're sending, and let the client browser determine how it should be handled.
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
I would definitely agree its best not to override the browsers preferences, but I just do what I'm told. Initially I thought this just wasn't possible (and I guess technically I was right).
I'll give your suggestion a try though. Thanks again.

-Tad
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
Well that works really well, except for files with spaces in the name. I end up with 0 byte files.
I'm thinking it has something to do with this part of the code:



The spaces on the actual page are converted into +'s
I think the request.getParameter("url") is turning the +'s back into spaces and that this maybe causing the problem trying to create a URL object from a string thats got spaces in it?

-Tad
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi Ted

Yes there is a way to open every file using a down load dialog box . There is a header in the response on setting this even html,txt file will be forced as download .

response.setContentType("application/x-download");

and coming to giving direct link to the document is not a good idea as u cant catch the filenotfound exeception(ofcourse u can handle that declaritively) and even this reveals the document path to the client which hampers the security of ur web application .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File downloads...