aspose file tools*
The moose likes Servlets and the fly likes problem with download dialog box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with download dialog box" Watch "problem with download dialog box" New topic
Author

problem with download dialog box

Conie Ooi
Ranch Hand

Joined: Jun 09, 2002
Posts: 31
Hi, I'm trying to download files from the server side to the client. When the user click at the link to the file, I want the downloading dialog box to appear so that the user can save it to anywhere he/she likes. But my problme now is I can't.
the following is my coding:
String filename = request.getParameter("filename");
String folder = "C:\\test\\" + filename;
System.out.println("foder is " + folder);
response.setContentType("text/plain");
response.setHeader("Content-Disposition","attachment; filename=;");
ServletOutputStream stream = response.getOutputStream();
BufferedInputStream fif =
new BufferedInputStream(new FileInputStream(folder));
int data;
while((data = fif.read()) != -1) {
stream.write(data);
}
fif.close();
stream.close();

with the above code, the browser will open the file for me. When I hardcode the filename, let's say a.txt, the download dialog box appear...
I've stucked in this for quite sometime, do you know why this happen and how to relove it?
Any suggestion is highly appreciated...
regards
Mario Rendon
Greenhorn

Joined: Jul 11, 2002
Posts: 24
you have to change your content type..

the browser you are using get's the content type "text/plain" and handles it .
just change your this line in your code..

response.setContentType("text/plain");

to response.setContentType("application/download");
Conie Ooi
Ranch Hand

Joined: Jun 09, 2002
Posts: 31
Thanks a lot... now my problem solved...
There is another problem. When the download dialog box poped up, if I select to save the file, it works. The file is saved to the client side. But if i choose to open the file from the current location, I will receive another download dialog box, and i have to select open file from the current location again to open the file. Do you have any idea why and how to solve this problem?
Thanks a lot...
[ August 15, 2002: Message edited by: Conie Ooi ]
Mario Rendon
Greenhorn

Joined: Jul 11, 2002
Posts: 24
well..
this is a problem with the
different browsers..

1.- Solution to netscape. and any browsers that handle mimetipes Correctly ..
(except IE in some versions )

there's a mapping betwen mimetype and application that Handles it ..
as an example:
open Netscape choose Edit Menu and choose preferences.
in the Applications Part you will find a looong list of MimeTypes Extensions and the program that is used to handle the document..
well when i told you to use "Application/download" the browser found no application to handle the file so it prompts you what to do with the file ..
so problem solved ..
but in the case you want the REAL application to handle the file you should use the REAL mimetype the file you are sending
so if you want to force client download a pdf format file you sould use the mymetype "application/download" but if you desire to open the file you sould use "application/pdf" as the mimetype ..

IE has a different way to use mimetypes .
mymetipes are located inside of the Operating system . so you would find it on Control panel or IE's preferences depending wich version of SO your cleints got (9598 ME 2000 etc.)
IE would use Mimetypes only if the URL hasn't extension. as .exe .html or.txt if the URL has an extension it would give precedence to extension rather than the Mimetype .. in your case the sevlet Has not extension so Probably IE would use Mimetypes ..

as a conclusion is "browsers" option what to to with the file you are sending
you can "Suggest" the Browser what to do with the file using mimetypes; but at the end the browser's option what to do with the file so you can't
Assure in the server side how the Browser would handle the data you are sending
Mimetypes is a Declaration of the type of data you are sending; but not inforces how to manage it..

grettings ..
Conie Ooi
Ranch Hand

Joined: Jun 09, 2002
Posts: 31
Thanks a lot Mario..
Mario Rendon
Greenhorn

Joined: Jul 11, 2002
Posts: 24
UR Welcomed..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with download dialog box