You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ha...@informatiefabriek.nl on 2003/04/24 15:37:35 UTC

downloading files

Hi all,

I have a struts application which shows a list of files which are stored 
somewhere on my filesystem (on the server) outside my application server.
When a user clicks on the link of a file the file should be downloaded and 
displayed in the browser (if possible). (Most files are PDF documents by 
the way).

Therefore i coded the following action:

....
InputStream is =
        new BufferedInputStream(
                new FileInputStream(valueObj.getPath()));
 
File f = new File(valueObj.getPath());
OutputStream os = response.getOutputStream();
response.setContentType("application/pdf");
byte[] buffer = new byte[1024]; /*or whatever size*/
int read = is.read(buffer);
while (read >= 0) {
        if (read > 0)
                os.write(buffer, 0, read);
                read = is.read(buffer);
        }

is.close();
os.close();
.....

This code works, but when the user clicks the 'save' button in the reader 
application (Adobe Acrobat) the file name looks something like: 
/show_pdf.do.pdf.

This ofcourse is incorrect. It should be the name of the original file. 

Therefore I added the following line of code:

response.setHeader("Content-Disposition","attachment; filename=\"" +
        f.getName() + "\";");


This changes the behaviour of my browser. Now it doesn't open it anymore 
inside the browser itself. But, now there is 'save-dialog'. When I use the 
file-save dialog the file name is correct.
But, when I try a MacIntosh (OSX with IE 5.2), the filename still isn't 
correct. (It still uses show_pdf.do.pdf). 

What is the correct way of doing this? Maybe there is something I can use 
from the upload package?

Thanks,

Harm de Laat
Informatiefabriek


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: downloading files

Posted by ha...@informatiefabriek.nl.
If I change it to 'inline' the browser does open the file embedded. 
But, when I click the 'save' button in the embedded acrobat reader the 
filename is incorrect. It is now the name of the action:
show_pdf.do.

So that does not work.... (I've tested this on mozilla 1.3 and IE 5.5 on 
windows).

Thanks,

Harm de Laat
Informatiefabriek




Dan Allen <da...@mojavelinux.com> 
04/24/2003 07:28 PM
Please respond to
"Struts Users Mailing List" <st...@jakarta.apache.org>


To
Struts Users Mailing List <st...@jakarta.apache.org>
cc

Subject
Re: downloading files







harm@informatiefabriek.nl (harm@informatiefabriek.nl) wrote:

> Hi all,
> 
> I have a struts application which shows a list of files which are stored 

> somewhere on my filesystem (on the server) outside my application 
server.
> When a user clicks on the link of a file the file should be downloaded 
and 
> displayed in the browser (if possible). (Most files are PDF documents by 

> the way).
> 
> Therefore i coded the following action:
> 
> ....
> InputStream is =
>         new BufferedInputStream(
>                 new FileInputStream(valueObj.getPath()));
> 
> File f = new File(valueObj.getPath());
> OutputStream os = response.getOutputStream();
> response.setContentType("application/pdf");
> byte[] buffer = new byte[1024]; /*or whatever size*/
> int read = is.read(buffer);
> while (read >= 0) {
>         if (read > 0)
>                 os.write(buffer, 0, read);
>                 read = is.read(buffer);
>         }
> 
> is.close();
> os.close();
> .....
> 
> This code works, but when the user clicks the 'save' button in the 
reader 
> application (Adobe Acrobat) the file name looks something like: 
> /show_pdf.do.pdf.
> 
> This ofcourse is incorrect. It should be the name of the original file. 
> 
> Therefore I added the following line of code:
> 
> response.setHeader("Content-Disposition","attachment; filename=\"" +
>         f.getName() + "\";");

--------------------------------------------^^^^^^^^^^
change that to inline to get it to display it embedded if possible.
You are forcing a save dialog.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Real programmers don't change their wardrobe too often: there 
are no clothes stores that are open at two o'clock in the morning. 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: downloading files

Posted by Dan Allen <da...@mojavelinux.com>.
harm@informatiefabriek.nl (harm@informatiefabriek.nl) wrote:

> Hi all,
> 
> I have a struts application which shows a list of files which are stored 
> somewhere on my filesystem (on the server) outside my application server.
> When a user clicks on the link of a file the file should be downloaded and 
> displayed in the browser (if possible). (Most files are PDF documents by 
> the way).
> 
> Therefore i coded the following action:
> 
> ....
> InputStream is =
>         new BufferedInputStream(
>                 new FileInputStream(valueObj.getPath()));
>  
> File f = new File(valueObj.getPath());
> OutputStream os = response.getOutputStream();
> response.setContentType("application/pdf");
> byte[] buffer = new byte[1024]; /*or whatever size*/
> int read = is.read(buffer);
> while (read >= 0) {
>         if (read > 0)
>                 os.write(buffer, 0, read);
>                 read = is.read(buffer);
>         }
> 
> is.close();
> os.close();
> .....
> 
> This code works, but when the user clicks the 'save' button in the reader 
> application (Adobe Acrobat) the file name looks something like: 
> /show_pdf.do.pdf.
> 
> This ofcourse is incorrect. It should be the name of the original file. 
> 
> Therefore I added the following line of code:
> 
> response.setHeader("Content-Disposition","attachment; filename=\"" +
>         f.getName() + "\";");

--------------------------------------------^^^^^^^^^^
change that to inline to get it to display it embedded if possible.
You are forcing a save dialog.

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Real programmers don't change their wardrobe too often: there 
are no clothes stores that are open at two o'clock in the morning. 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org