Re: How to force a file download to save
Leviaguirre Queral José Luis <[email protected]>
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Message-ID | <1784B9456C4D094C87502782845C9315C2F1A7@is~tmxmailmex5.intranet.telmex.com> |
Another way is to set Content-Disposition header to "octect-stream". Here is an example in JSP that reads a file and sends it to the client:
<%
//The server will tweak the HTTP contentType header
response.setContentType("APPLICATION/OCTET-STREAM");
String save_file_as = "clientfilename.csv"; //the user will see this filename to save as
response.setHeader("Content-Disposition","attachment; filename=\""+save_file_as+"\"");
//open file to download and read byte per byte until EOF and close.
fileInputStream = new java.io.FileInputStream(file2download);
int i;while ((i=fileInputStream.read()) != -1){
out.write(i); }
fileInputStream.close();
out.close();
%>
Hope it helps,
Jose Luis Leviaguirre
-----Mensaje original-----
De: Jean-Christophe CHARVY [mailto:[email protected]]
Enviado el: Lunes, 26 de Mayo de 2003 09:43 a.m.
Para: [email protected]
Asunto: RE: How to force a file download to save
Hi,
If you manage download through servlets, you can specify which behaviour the client browser has to follow.
To force the file download to save, use :
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
JC
http://jc.charvy.free.fr/
---------- Entête Initiale -----------
De : "A mailing list for discussion about Sun Microsystem's Java Servlet API Technology." <[email protected]>
A : [email protected]
Copie :
Date : Wed, 21 May 2003 10:47:47 -0400
Objet : How to force a file download to save
> Group,
> can someone tell me what the options are for forcing a file
> download to pop up a save dialogue.
>
> What I want to do is force the save dialogue to open for any of the
> files available from a particular site for download, including our xml,
> txt and csv files.
>
> As far as I know, I can either:
> 1. Change my server mimetypes so that it defaults to sending my file
> with a content type of e.g. "bad/type"
> 2. Manually alter the response object to set the content type to e.g.
> "bad/type".
>
> Are there any other ways that I can do this?
> Cheers,
> Neil.
>
> ___________________________________________________________________________
> To unsubscribe, send email to [email protected] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>
------------------------------------------
Faites un voeu et puis Voila ! www.voila.fr
___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html