Click to See Complete Forum and Search --> : send file to browser
CrowAbyss
March 29th, 2001, 12:16 PM
Hi,
simple question:
How can I send a file to a browser?
I do the following right now:
ServletOutputStream sos=response.getOutputStream();
response.setContentType("application/xxx");
response.addHeader("filename","xxx");
byte[] bbb=new byte[1024];
for (int i=0;i<1024;i++) bbb[i]='a';
sos.write(bbb);
But I also want to set a filename. How do I have to do that?
Do I have to set something in the header of the response?
Give me some code example if possible.
Thanx, Andy
Norm
March 29th, 2001, 04:56 PM
One way might be to append the filename on the end of the URL that the browser sends to the server. The server should pass it as extra path info to the servlet and I think that the browser expects to get that file back. Otherwise I think there are ways to put filenames in the Response header. You'll have to read an HTML book to see what they are, or ask on the HTML forum.
Norm
dlorde
March 30th, 2001, 08:11 AM
If you want to have the browser popup a file download dialog, you can do something like this: String sourceFile = "SourceFileName.xxx";
String clientFile = "DestFileName.xxx";
byte[] fileBuffer = getFileBytes(sourceFile);
// Set HttpServletResponse details
response.setContentType("application/x-filler");
response.setHeader("Content-Disposition", "attachment; filename=" + clientFile);
// Read from file into servlet output stream
ServletOutputStream stream = response.getOutputStream();
BufferedInputStream fif = new BufferedInputStream(new ByteArrayInputStream(fileBuffer));
int data = 0;
while((data = fif.read()) != -1)
{
stream.write(data);
}
fif.close();
stream.close();
Note that there is a bug in MS Internet Explorer 5.5 that causes the browser pop-up to specify the HTML page URL instead of the filename.
Dave
To email me remove '_spamjam' from my email address
Saye
March 30th, 2001, 02:47 PM
Can you send the file directly into the user hardrive without have the Pop-Up window to appear ??
I try to write a Serlet that transfer a bunch of requested files at once without the user having to
to answer to the Pop-Up window...
Thanks in Advance
dlorde
April 2nd, 2001, 05:11 AM
I don't think this is possible for security reasons. The browser wouldn't allow it. Imagine if a site you visited could secretly download a file to your disk without your knowing about it!
If you used a trusted applet, you could do this, but that's another story...
Dave
To email me remove '_spamjam' from my email address
Norm
April 2nd, 2001, 11:44 AM
According to my HTML book: HTML Sourcebook by Ian Graham, Content-disposition: is only understood by Netscape. The browser generally uses the last part of the URL as the default filename. I guess that's what you're seeing IE do.
When using a servlet, you can add extra path info with the filename on the URL for the browser to use to set the filename. This works in both IE and Netscape.
Norm
dlorde
April 2nd, 2001, 12:25 PM
The HTML Sourcebook by Ian Graham is incorrect in this respect. There was a problem with IE 4.01 somethimes attempting to open the file without prompting (see http://support.microsoft.com/support/kb/articles/Q182/3/15.ASP?LN=EN-US&SD=gn&FR=0&qry=file%20download%20attachment%20content-disposition&rnk=1&src=DHCS_MSPSS_gn_SRCH&SPR=IEP), but our customers have been successfully using this method to download files from a remote FTP site using both Netscape and MS Internet Explorer. However, those that have upgraded to MS IE 5.5 have found the incorrect pop-up problem. A browse through the Microsoft support database shows that they acknowledge the bug in IE 5.5 and suggest workarounds. See: http://support.microsoft.com/support/kb/articles/Q279/6/67.ASP?LN=EN-US&SD=gn&FR=0&qry=file%20download%20attachment%20content-disposition&rnk=4&src=DHCS_MSPSS_gn_SRCH&SPR=IEP
and:
http://search.support.microsoft.com/kb/psssearch.asp?SPR=iep&T=B&KT=ALL&T1=7d&LQ=file+download+attachment+content-disposition&PQ=0&S=F&A=T&DU=C&FR=0&D=ie_dev&LPR=&LNG=ENG&VR=http%3A%2F%2Fsupport.microsoft.com%2Fsupport%3Bhttp%3A%2F%2Fsupport.microsoft.com%2Fservicedesks%2Fwebcasts%3Bhttp%3A%2F%2Fsupport.microsoft.com%2Fhighlights&CAT=Support&VRL=ENG&SA=GN&Go.x=23&Go.y=20
Dave
To email me remove '_spamjam' from my email address
Norm
April 2nd, 2001, 05:59 PM
Thanks for the info. I don't do this for a living and really hate reading documentation online, so I only know what I see directly or read from a book which is always at least 2 years behind the times.
Norm
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.