• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with download dialog box

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Mario..
 
Mario Rendon
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UR Welcomed..
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic