You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Timothy Orme <to...@genome.med.harvard.edu> on 2009/07/24 16:41:02 UTC

Downloading a Streamed File

Hello,

	I did some digging on this but couldn't find anything. I have an action which is returning a streamed result. I create a zip file in memory and stream it out to the user. This works fine when the 
user simply clicks the link. I get the correct file, with the right name. However, as of right now, if the user right clicks on the link and goes to "Save Link As", the link comes up to save as the 
action name instead of the zip file, and it gives the following error:

Exception java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the &lt;param name=&quot;inputName&quot;&gt; tag specified 
for this action.

However, if I click the link it works fine.

The code in my action looks like:

	public InputStream getInputStream() throws Exception{
		
		String contentType="application/octet-stream";
		
		Result result = ActionContext.getContext().getActionInvocation().getResult();
		if(result != null && result instanceof StreamResult){
			StreamResult streamResult = (StreamResult)result;
			streamResult.setContentType(contentType);
			streamResult.setContentDisposition("attachment; filename=\"" + plateName + ".zip\"");
		}
		
		return new ByteArrayInputStream(bos.toByteArray());
		
	}

Any suggestions? Ideally I'd like the user to be able to right click and save the link and get the correct zip file. I feel as though I've seen this done on other sites, but can't find an example.

Thanks,
Tim


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


Re: Downloading a Streamed File

Posted by Timothy Orme <to...@genome.med.harvard.edu>.
And some further digging for those who are interested:

https://bugzilla.mozilla.org/show_bug.cgi?id=299372#c89

Apparently just a "bug" in firefox, though they have no intentions of fixing it.

Another link with more info about it in the wild:

http://drupal.org/node/74983

Thanks,
Tim Orme

Timothy Orme wrote:
> Right, I figured that it wasn't a struts specific issue, but thought 
> that I might have been using the wrong content disposition or something 
> along those lines.
> 
> However, upon further prodding, it seems that this is more an issue of 
> response time with the browser.
> 
> I found that the behavior I was seeking worked with the JasperReports 
> plugin. The user clicked on a .action link and got a PDF. Right clicking 
> to save as gave them the .pdf as well.
> 
> However, if I set a break point in my report generation code to halt it, 
> the browser forces the "Save as" dialog to appear, but at this point it 
> hasn't received any response yet, so it doesn't know what the content 
> disposition, and instead of the usual filename being set to a PDF, I get 
> the same "bad" behavior of it saving as the action name. This kind of 
> makes sense from the browsers perspective though, since it'd be odd to 
> click to save a link, only to have the browser apparently hang while the 
> dialog waits for a response.
> 
> My problem really then, is that my action to make a zip simply takes too 
> long to do its thing, and the browser refuses to wait.
> 
> I'm not sure if theres really a workaround for this, other than writing 
> to disk and linking directly to the file.
> 
> I'm not sure what other parts of my config you need, but heres the 
> struts.xml for that action:
> 
> <action name="DownloadFile" class="action.DownloadFileAction">
>     <result name="success" type="stream">
>         <param name="inputName">inputStream</param>
>         <param name="contentType">application/octet-stream</param>
>         <param name="contentDisposition">attachment; 
> filename="${fileName}.zip"</param>
>     </result>
>     
>     <result name="input" type="redirect">ViewDownloadForm.action</result>
> </action>
> 
> 
> Thanks,
> Tim
> 
> Dale Newfield wrote:
>> Timothy Orme wrote:
>>> I still have the same issue though. Right clicking the link doesn't 
>>> work and the filename is still listed as the action.
>>
>> As far as struts is concerned, there's no difference between a request 
>> that's going to be displayed in a browser window and a request that's 
>> going to be saved by the browser to a file.  I'd start by looking at 
>> URLs and logs and seeing how those requests differ from one another. 
>> For example, make sure your browser (or proxy or something) isn't 
>> showing you a cached version of the left-click result.
>>
>> If you want help, you need to provide enough of your configuration for 
>> us to diagnose what's going on, assuming the request is being mapped 
>> to that action.
>>
>> -Dale
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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


Re: Downloading a Streamed File

Posted by Dale Newfield <da...@newfield.org>.
Timothy Orme wrote:
> I'm not sure if theres really a workaround for this, other than writing 
> to disk and linking directly to the file.

Or monkeying with the url (and maybe the extensions that map to struts) 
so that you can make the url of the action end with .../filename.zip .

-Dale

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


Re: Downloading a Streamed File

Posted by Dale Newfield <da...@newfield.org>.
Timothy Orme wrote:
> My problem really then, is that my action to make a zip simply takes too 
> long to do its thing, and the browser refuses to wait.

I don't know if this'll help you, but you could create and use a 
modified version of StreamResult that makes many more of the 
response.addHeader() and response.setXxx() calls before the InputStream 
is requested from your action (which I believe is where you're saying 
your action takes too long).  This *might* enable more of the header to 
be sent out to the browser earlier...

>         <param name="contentDisposition">attachment; 
> filename="${fileName}.zip"</param>

It appears that you've got a space after that semicolon, which from my 
reading of the HTTP specification shouldn't be there.  Presumably most 
browsers don't care, but...

Oh, and now in your struts.xml you can now use ognl's "%{}" instead of 
jstl's "${}".

-Dale

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


Re: Downloading a Streamed File

Posted by Timothy Orme <to...@genome.med.harvard.edu>.
Right, I figured that it wasn't a struts specific issue, but thought that I might have been using the wrong content disposition or something along those lines.

However, upon further prodding, it seems that this is more an issue of response time with the browser.

I found that the behavior I was seeking worked with the JasperReports plugin. The user clicked on a .action link and got a PDF. Right clicking to save as gave them the .pdf as well.

However, if I set a break point in my report generation code to halt it, the browser forces the "Save as" dialog to appear, but at this point it hasn't received any response yet, so it doesn't know 
what the content disposition, and instead of the usual filename being set to a PDF, I get the same "bad" behavior of it saving as the action name. This kind of makes sense from the browsers 
perspective though, since it'd be odd to click to save a link, only to have the browser apparently hang while the dialog waits for a response.

My problem really then, is that my action to make a zip simply takes too long to do its thing, and the browser refuses to wait.

I'm not sure if theres really a workaround for this, other than writing to disk and linking directly to the file.

I'm not sure what other parts of my config you need, but heres the struts.xml for that action:

<action name="DownloadFile" class="action.DownloadFileAction">
	<result name="success" type="stream">
		<param name="inputName">inputStream</param>
		<param name="contentType">application/octet-stream</param>
		<param name="contentDisposition">attachment; filename="${fileName}.zip"</param>
	</result>
	
	<result name="input" type="redirect">ViewDownloadForm.action</result>
</action>


Thanks,
Tim

Dale Newfield wrote:
> Timothy Orme wrote:
>> I still have the same issue though. Right clicking the link doesn't 
>> work and the filename is still listed as the action.
> 
> As far as struts is concerned, there's no difference between a request 
> that's going to be displayed in a browser window and a request that's 
> going to be saved by the browser to a file.  I'd start by looking at 
> URLs and logs and seeing how those requests differ from one another. For 
> example, make sure your browser (or proxy or something) isn't showing 
> you a cached version of the left-click result.
> 
> If you want help, you need to provide enough of your configuration for 
> us to diagnose what's going on, assuming the request is being mapped to 
> that action.
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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


RE: Downloading a Streamed File

Posted by Martin Gainty <mg...@hotmail.com>.
good call dale

although the regular HTTP 1.1 (coyote) connection easily pushes capacity envelope with streaming 
at last count was 8192 ..stop...go up to site ..get next chunk

there are *newer Asynch connectors* coming down the pike that will expand capacity for large chunked/unchunked blobs xfers...as a test site i would try the offering here (Author recites preface as a streaming video)
http://www.hup.harvard.edu/catalog/DANAFR.html

good to hear the crimson point of view

Martin Gainty 
______________________________________________ 
Note de déni et de confidentialité

Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Fri, 24 Jul 2009 13:53:36 -0400
> From: dale@newfield.org
> To: user@struts.apache.org
> Subject: Re: Downloading a Streamed File
> 
> Timothy Orme wrote:
> > I still have the same issue though. Right clicking the link 
> > doesn't work and the filename is still listed as the action.
> 
> As far as struts is concerned, there's no difference between a request 
> that's going to be displayed in a browser window and a request that's 
> going to be saved by the browser to a file.  I'd start by looking at 
> URLs and logs and seeing how those requests differ from one another. 
> For example, make sure your browser (or proxy or something) isn't 
> showing you a cached version of the left-click result.
> 
> If you want help, you need to provide enough of your configuration for 
> us to diagnose what's going on, assuming the request is being mapped to 
> that action.
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 

_________________________________________________________________
NEW mobile Hotmail. Optimized for YOUR phone.  Click here.
http://windowslive.com/Mobile?ocid=TXT_TAGLM_WL_CS_MB_new_hotmail_072009

Re: Downloading a Streamed File

Posted by Dale Newfield <da...@newfield.org>.
Timothy Orme wrote:
> I still have the same issue though. Right clicking the link 
> doesn't work and the filename is still listed as the action.

As far as struts is concerned, there's no difference between a request 
that's going to be displayed in a browser window and a request that's 
going to be saved by the browser to a file.  I'd start by looking at 
URLs and logs and seeing how those requests differ from one another. 
For example, make sure your browser (or proxy or something) isn't 
showing you a cached version of the left-click result.

If you want help, you need to provide enough of your configuration for 
us to diagnose what's going on, assuming the request is being mapped to 
that action.

-Dale

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


Re: Downloading a Streamed File

Posted by Timothy Orme <to...@genome.med.harvard.edu>.
Hey,

	Not sure where I got this from, but I've corrected it so that all the properties are in the struts.xml instead of modifying it in the code. I still have the same issue though. Right clicking the link 
doesn't work and the filename is still listed as the action.

Thanks,
Tim

Dale Newfield wrote:
> Why are you trying to modify the result once it's already running rather 
> than configuring it in your struts.xml?
> 
> http://struts.apache.org/2.1.6/struts2-core/apidocs/org/apache/struts2/dispatcher/StreamResult.html 
> 
> 
> Note, there's a bug in that documentation that'll be fixed in the next 
> release, which you've already figured: instead of 
> 'filename="document.pdf"' it should be 
> 'attachment;filename="document.pdf"'.
> 
> -Dale
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 


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


Re: Downloading a Streamed File

Posted by Dale Newfield <da...@newfield.org>.
Why are you trying to modify the result once it's already running rather 
than configuring it in your struts.xml?

http://struts.apache.org/2.1.6/struts2-core/apidocs/org/apache/struts2/dispatcher/StreamResult.html

Note, there's a bug in that documentation that'll be fixed in the next 
release, which you've already figured: instead of 
'filename="document.pdf"' it should be 'attachment;filename="document.pdf"'.

-Dale

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