You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Edoardo Campagnano <ed...@dappolonia.it> on 2007/01/09 15:29:19 UTC

download a file

Hi to all,

here's the problem: I have a file in my DB as a byte[] array and i want a
link to permit the user to download the file. First, I'm able to ricreate a
disk-version of the file using FileOutputStream, the file is good (I can
open it on the server) but is generated on request thus i can't use it as an
asset in tapestry, how can I permit the user to obtain it?

 

Thanks a lot, Edoardo

 


Re: AW: download a file

Posted by Dennis Sinelnikov <de...@augustschell.com>.
Holger,

This is what I did, perhaps it'll give you some hints on how to fix your 
problem:

SomePage.html
...
<input type="button" value="Download PDF Report" onClick="serviceLink()"/>
...
<script type="text/javascript">
function serviceLink() {
   window.location='<span jwcid="@Insert" value="ognl:serviceURL" 
raw="true"/>';
</script>
...

SomePage.java:
...
@InjectObject("engine-service:download")
public abstract IEngineService getDownloadService();

public String getServiceURL()
{
   ILink link = this.getDownloadService().getLink(false, new 
String[]{...User Report Params...});
         return link.getAbsoluteURL();
}
...

I believe other code you can gather from Kent's book...

hth,
Dennis

Holger Stolzenberg wrote:
> Only partially! But actually I don´t know what the real problem is, so I thought that maybe in your code I could figure out.
> 
> We have the following issue (using tap 4.0.2 with tacos):
> 
> I have form which is submit by a @Submit. In the form the user can edit parameters that are needed for report creation.
> The @Submit as a action binding which is calling a listener method. In this listener method we want to generate a report with the parameters defined in the form. Therefore we are using an custom IEngineService that returns the PDF to the browser.
> 
> Everything works fine but after the report has been created and has been sent to the browser as PDF, a Exception is thrown saying that getOutputStream() has already been called. We have read the section "Downloading ..." in Kents book, but at the moment we have no idea where to go to stop this exception.
> 
> -----Ursprüngliche Nachricht-----
> Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com] Im Auftrag von James Carman
> Gesendet: Mittwoch, 10. Januar 2007 14:42
> An: Tapestry users
> Betreff: Re: download a file
> 
> Shing's code didn't show you what you needed?
> 
> 
> On 1/10/07, Holger Stolzenberg <h....@ewerk.com> wrote:
>> I am very intrested! Please share your code
>>
>> -----Ursprüngliche Nachricht-----
>> Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com] Im Auftrag von 
>> James Carman
>> Gesendet: Mittwoch, 10. Januar 2007 02:32
>> An: Tapestry users
>> Betreff: Re: download a file
>>
>> Let me know if Shing's example (it looks to be pretty good) isn't enough for you.  The code I wrote was for a client, but there's really not much "clientness" to it, so I could probably share it.  All it does is take image data out of a blob in the database and stream it back.  Not rocket science by any means.  The hardest part of it is the "servicey" bits.
>>
>> p.s. For the record, that's two phrases that I coined in the same 
>> email :-)
>>
>>
>>
>>
>> On 1/9/07, Robert J. Walker <rw...@mshare.net> wrote:
>>> James Carman wrote:
>>>
>>>> I did this by creating my own "service" in T4.
>>> Have some code to share? I'm sure we'd all love to see it.
>>>
>>>
>>> --------------------------------------------------------------------
>>> - To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: AW: download a file

Posted by Shing Hing Man <ma...@yahoo.com>.
The following is only an idea which I have not tried
it.

After the form is submitted and data validated,  
throws a org.apache.tapestry.RedirectException
to go to your custom service.
eg :
throw new RedirectException(url to your service);

http://tapestry.apache.org/tapestry4/tapestry/apidocs/index.html


You can either pass the form data  in the url of 
your service  or pass a 'key' for the form data and
use the key to retrieve the data in your service's
service method.
 
Shing


--- Holger Stolzenberg <h....@ewerk.com>
wrote:

> Only partially! But actually I don´t know what the
> real problem is, so I thought that maybe in your
> code I could figure out.
> 
> We have the following issue (using tap 4.0.2 with
> tacos):
> 
> I have form which is submit by a @Submit. In the
> form the user can edit parameters that are needed
> for report creation.
> The @Submit as a action binding which is calling a
> listener method. In this listener method we want to
> generate a report with the parameters defined in the
> form. Therefore we are using an custom
> IEngineService that returns the PDF to the browser.
> 
> Everything works fine but after the report has been
> created and has been sent to the browser as PDF, a
> Exception is thrown saying that getOutputStream()
> has already been called. We have read the section
> "Downloading ..." in Kents book, but at the moment
> we have no idea where to go to stop this exception.
> 
> -----Ursprüngliche Nachricht-----
> Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com]
> Im Auftrag von James Carman
> Gesendet: Mittwoch, 10. Januar 2007 14:42
> An: Tapestry users
> Betreff: Re: download a file
> 
> Shing's code didn't show you what you needed?
> 
> 
> On 1/10/07, Holger Stolzenberg
> <h....@ewerk.com> wrote:
> > I am very intrested! Please share your code
> >
> > -----Ursprüngliche Nachricht-----
> > Von: jwcarman@gmail.com
> [mailto:jwcarman@gmail.com] Im Auftrag von 
> > James Carman
> > Gesendet: Mittwoch, 10. Januar 2007 02:32
> > An: Tapestry users
> > Betreff: Re: download a file
> >
> > Let me know if Shing's example (it looks to be
> pretty good) isn't enough for you.  The code I wrote
> was for a client, but there's really not much
> "clientness" to it, so I could probably share it. 
> All it does is take image data out of a blob in the
> database and stream it back.  Not rocket science by
> any means.  The hardest part of it is the "servicey"
> bits.
> >
> > p.s. For the record, that's two phrases that I
> coined in the same 
> > email :-)
> >
> >
> >
> >
> > On 1/9/07, Robert J. Walker <rw...@mshare.net>
> wrote:
> > > James Carman wrote:
> > >
> > > > I did this by creating my own "service" in T4.
> > >
> > > Have some code to share? I'm sure we'd all love
> to see it.
> > >
> > >
> > >
>
--------------------------------------------------------------------
> > > - To unsubscribe, e-mail:
> users-unsubscribe@tapestry.apache.org
> > > For additional commands, e-mail:
> users-help@tapestry.apache.org
> > >
> > >
> > >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail:
> users-help@tapestry.apache.org
> >
> >
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail:
> users-help@tapestry.apache.org
> >
> >
> >
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail:
> users-help@tapestry.apache.org
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail:
> users-help@tapestry.apache.org
> 
> 


Home page :
  http://uk.geocities.com/matmsh/index.html


		
___________________________________________________________ 
The all-new Yahoo! Mail goes wherever you go - free your email address from your Internet provider. http://uk.docs.yahoo.com/nowyoucan.html

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


AW: download a file

Posted by Holger Stolzenberg <h....@ewerk.com>.
Only partially! But actually I don´t know what the real problem is, so I thought that maybe in your code I could figure out.

We have the following issue (using tap 4.0.2 with tacos):

I have form which is submit by a @Submit. In the form the user can edit parameters that are needed for report creation.
The @Submit as a action binding which is calling a listener method. In this listener method we want to generate a report with the parameters defined in the form. Therefore we are using an custom IEngineService that returns the PDF to the browser.

Everything works fine but after the report has been created and has been sent to the browser as PDF, a Exception is thrown saying that getOutputStream() has already been called. We have read the section "Downloading ..." in Kents book, but at the moment we have no idea where to go to stop this exception.

-----Ursprüngliche Nachricht-----
Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com] Im Auftrag von James Carman
Gesendet: Mittwoch, 10. Januar 2007 14:42
An: Tapestry users
Betreff: Re: download a file

Shing's code didn't show you what you needed?


On 1/10/07, Holger Stolzenberg <h....@ewerk.com> wrote:
> I am very intrested! Please share your code
>
> -----Ursprüngliche Nachricht-----
> Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com] Im Auftrag von 
> James Carman
> Gesendet: Mittwoch, 10. Januar 2007 02:32
> An: Tapestry users
> Betreff: Re: download a file
>
> Let me know if Shing's example (it looks to be pretty good) isn't enough for you.  The code I wrote was for a client, but there's really not much "clientness" to it, so I could probably share it.  All it does is take image data out of a blob in the database and stream it back.  Not rocket science by any means.  The hardest part of it is the "servicey" bits.
>
> p.s. For the record, that's two phrases that I coined in the same 
> email :-)
>
>
>
>
> On 1/9/07, Robert J. Walker <rw...@mshare.net> wrote:
> > James Carman wrote:
> >
> > > I did this by creating my own "service" in T4.
> >
> > Have some code to share? I'm sure we'd all love to see it.
> >
> >
> > --------------------------------------------------------------------
> > - To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: download a file

Posted by James Carman <ja...@carmanconsulting.com>.
Shing's code didn't show you what you needed?


On 1/10/07, Holger Stolzenberg <h....@ewerk.com> wrote:
> I am very intrested! Please share your code
>
> -----Ursprüngliche Nachricht-----
> Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com] Im Auftrag von James Carman
> Gesendet: Mittwoch, 10. Januar 2007 02:32
> An: Tapestry users
> Betreff: Re: download a file
>
> Let me know if Shing's example (it looks to be pretty good) isn't enough for you.  The code I wrote was for a client, but there's really not much "clientness" to it, so I could probably share it.  All it does is take image data out of a blob in the database and stream it back.  Not rocket science by any means.  The hardest part of it is the "servicey" bits.
>
> p.s. For the record, that's two phrases that I coined in the same email :-)
>
>
>
>
> On 1/9/07, Robert J. Walker <rw...@mshare.net> wrote:
> > James Carman wrote:
> >
> > > I did this by creating my own "service" in T4.
> >
> > Have some code to share? I'm sure we'd all love to see it.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


AW: download a file

Posted by Holger Stolzenberg <h....@ewerk.com>.
I am very intrested! Please share your code

-----Ursprüngliche Nachricht-----
Von: jwcarman@gmail.com [mailto:jwcarman@gmail.com] Im Auftrag von James Carman
Gesendet: Mittwoch, 10. Januar 2007 02:32
An: Tapestry users
Betreff: Re: download a file

Let me know if Shing's example (it looks to be pretty good) isn't enough for you.  The code I wrote was for a client, but there's really not much "clientness" to it, so I could probably share it.  All it does is take image data out of a blob in the database and stream it back.  Not rocket science by any means.  The hardest part of it is the "servicey" bits.

p.s. For the record, that's two phrases that I coined in the same email :-)




On 1/9/07, Robert J. Walker <rw...@mshare.net> wrote:
> James Carman wrote:
>
> > I did this by creating my own "service" in T4.
>
> Have some code to share? I'm sure we'd all love to see it.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: download a file

Posted by James Carman <ja...@carmanconsulting.com>.
Let me know if Shing's example (it looks to be pretty good) isn't
enough for you.  The code I wrote was for a client, but there's really
not much "clientness" to it, so I could probably share it.  All it
does is take image data out of a blob in the database and stream it
back.  Not rocket science by any means.  The hardest part of it is the
"servicey" bits.

p.s. For the record, that's two phrases that I coined in the same email :-)




On 1/9/07, Robert J. Walker <rw...@mshare.net> wrote:
> James Carman wrote:
>
> > I did this by creating my own "service" in T4.
>
> Have some code to share? I'm sure we'd all love to see it.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: download a file

Posted by andyhot <an...@di.uoa.gr>.
Robert J. Walker wrote:
> James Carman wrote:
>
>   
>> I did this by creating my own "service" in T4.
>>     
>
> Have some code to share? I'm sure we'd all love to see it.
>   

Shing has put up this nice page that details many such things

http://lombok.demon.co.uk/tapestry4Demo/app

>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>
>
>   


-- 
Andreas Andreou - andyhot@apache.org - http://andyhot.di.uoa.gr
Tapestry / Tacos developer
Open Source / J2EE Consulting 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: download a file

Posted by "Robert J. Walker" <rw...@mshare.net>.
James Carman wrote:

> I did this by creating my own "service" in T4.

Have some code to share? I'm sure we'd all love to see it.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: download a file

Posted by James Carman <ja...@carmanconsulting.com>.
I did this by creating my own "service" in T4.


On 1/9/07, Dennis Sinelnikov <de...@augustschell.com> wrote:
> Robert's Tapestry 3's method works, but Tapestry 4's method might cause
> some problems, at least it did when I tried it. I believe it has to do
> with closing the response outputstream before rewind or the rest of
> rendering is complete.  User will be able to download the file
> successfully, but you'll see an error in your logs about OutputStream is
> already closed (or something along those lines).  To get around this
> error, I had to implement my own download engine service, as Shing
> suggested.
>
> Dennis
>
> Robert J. Walker wrote:
> > Here's a method that will do what you're asking:
> >
> > // Tapestry 3
> > protected void download(IRequestCycle cycle, ByteArrayOutputStream content, String contentType, String filename) throws IOException {
> >     HttpServletResponse response = cycle.getRequestContext().getResponse();
> >     response.setHeader("Content-disposition", "attachment; filename=" + filename);
> >     response.setContentType(contentType);
> >     response.setContentLength(content.size());
> >     response.getOutputStream().write(content.toByteArray());
> >     response.flushBuffer();
> >     response.getOutputStream().close();
> > }
> >
> > // Tapestry 4
> > protected void download(IRequestCycle cycle, ByteArrayOutputStream content, String contentType, String filename) throws IOException {
> >     WebResponse response = cycle.getInfrastructure().getResponse();
> >     response.setHeader("Content-disposition", "attachment; filename=" + filename);
> >     response.setIntHeader("Content-Length", content.size());
> >     OutputStream stream = response.getOutputStream(new ContentType(contentType));
> >     stream.write(content.toByteArray());
> >     stream.flush();
> >     stream.close();
> > }
> >
> > You should make sure that the filename you provide is compatible with the user's operating system, or you could run into problems. It's also advisable to make the file extension jive with the content type so that the user doesn't have to rename the file to open it. Hope this helps.
> >
> > Robert J. Walker
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: download a file

Posted by Dennis Sinelnikov <de...@augustschell.com>.
Robert's Tapestry 3's method works, but Tapestry 4's method might cause 
some problems, at least it did when I tried it. I believe it has to do 
with closing the response outputstream before rewind or the rest of 
rendering is complete.  User will be able to download the file 
successfully, but you'll see an error in your logs about OutputStream is 
already closed (or something along those lines).  To get around this 
error, I had to implement my own download engine service, as Shing 
suggested.

Dennis

Robert J. Walker wrote:
> Here's a method that will do what you're asking:
> 
> // Tapestry 3
> protected void download(IRequestCycle cycle, ByteArrayOutputStream content, String contentType, String filename) throws IOException {
>     HttpServletResponse response = cycle.getRequestContext().getResponse();
>     response.setHeader("Content-disposition", "attachment; filename=" + filename);
>     response.setContentType(contentType);
>     response.setContentLength(content.size());
>     response.getOutputStream().write(content.toByteArray());
>     response.flushBuffer();
>     response.getOutputStream().close();
> }
> 
> // Tapestry 4
> protected void download(IRequestCycle cycle, ByteArrayOutputStream content, String contentType, String filename) throws IOException {
>     WebResponse response = cycle.getInfrastructure().getResponse();
>     response.setHeader("Content-disposition", "attachment; filename=" + filename);
>     response.setIntHeader("Content-Length", content.size());
>     OutputStream stream = response.getOutputStream(new ContentType(contentType));
>     stream.write(content.toByteArray());
>     stream.flush();
>     stream.close();
> }
> 
> You should make sure that the filename you provide is compatible with the user's operating system, or you could run into problems. It's also advisable to make the file extension jive with the content type so that the user doesn't have to rename the file to open it. Hope this helps.
> 
> Robert J. Walker
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


RE: download a file

Posted by "Robert J. Walker" <rw...@mshare.net>.
Here's a method that will do what you're asking:

// Tapestry 3
protected void download(IRequestCycle cycle, ByteArrayOutputStream content, String contentType, String filename) throws IOException {
    HttpServletResponse response = cycle.getRequestContext().getResponse();
    response.setHeader("Content-disposition", "attachment; filename=" + filename);
    response.setContentType(contentType);
    response.setContentLength(content.size());
    response.getOutputStream().write(content.toByteArray());
    response.flushBuffer();
    response.getOutputStream().close();
}

// Tapestry 4
protected void download(IRequestCycle cycle, ByteArrayOutputStream content, String contentType, String filename) throws IOException {
    WebResponse response = cycle.getInfrastructure().getResponse();
    response.setHeader("Content-disposition", "attachment; filename=" + filename);
    response.setIntHeader("Content-Length", content.size());
    OutputStream stream = response.getOutputStream(new ContentType(contentType));
    stream.write(content.toByteArray());
    stream.flush();
    stream.close();
}

You should make sure that the filename you provide is compatible with the user's operating system, or you could run into problems. It's also advisable to make the file extension jive with the content type so that the user doesn't have to rename the file to open it. Hope this helps.

Robert J. Walker


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: download a file

Posted by Shing Hing Man <ma...@yahoo.com>.
A custom Tapestry engine service ?

Shing 

--- Edoardo Campagnano
<ed...@dappolonia.it> wrote:

> Hi to all,
> 
> here's the problem: I have a file in my DB as a
> byte[] array and i want a
> link to permit the user to download the file. First,
> I'm able to ricreate a
> disk-version of the file using FileOutputStream, the
> file is good (I can
> open it on the server) but is generated on request
> thus i can't use it as an
> asset in tapestry, how can I permit the user to
> obtain it?
> 
>  
> 
> Thanks a lot, Edoardo
> 
>  
> 
> 


Home page :
  http://uk.geocities.com/matmsh/index.html

Send instant messages to your online friends http://uk.messenger.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org