Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF streamed download on android device returns .htm file
    primarykey
    data
    text
    <p>I'm currently facing a weird problem in my webpage on android devices.</p> <p>What I want to do is to allow a user to download a pdf-file to his mobile device. Therefore I provide a download button as in the setup described <a href="https://stackoverflow.com/questions/9584736/jsf-file-download-nullpointerexception-with-view-parameter">here</a>.</p> <p>Everything works fine as long as I use my desktop browser *Mozilla Firefox 10.** but as soon as I change to my mobile device <em>(SGS II, Android vers. 2.3.5)</em> the result of the download depends on the browser-app I'm using.</p> <p><strong>Mozilla and Opera mobile:</strong><br> Both seem to be able to download the file correctly.</p> <p><strong>Any other browser-app (built-in, Dolphin HD,...):</strong><br> Downloads a file either named <code>&lt;filename&gt;.pdf</code> or <code>&lt;filename&gt;.htm</code> which both represent a <strong>.htm</strong>-file showing the html-source of the page.</p> <p><strong>What I've tried:</strong> </p> <ul> <li><p>Used the <code>StreamedContent</code> method out of the <em>PrimeFaces</em> library</p> <pre><code>public StreamedContent getFile() { // prepare file for download // reference webDAV directory and get file as stream this.file = new Helper().getWebDavFile(customerId, fileName); return file; } </code></pre></li> <li><p>Streamed the file manually to the page as described <a href="https://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean">here</a>. (Thx to BalusC)</p> <pre><code>public void download() throws IOException { byte[] is = new Helper().getWebDavFileManually(customerId, fileName); FacesContext fc = FacesContext.getCurrentInstance(); ExternalContext ec = fc.getExternalContext(); ec.responseReset(); ec.setResponseContentType("application/pdf"); ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName.toUpperCase() + "\""); OutputStream output = ec.getResponseOutputStream(); output.write(is); fc.responseComplete(); } </code></pre></li> <li><p>Set an <code>&lt;a href=""&gt;</code> to a local copy of the file.<br> (I'm currently using a <code>&lt;p:commandButton&gt;</code> so I've to use a method executing a redirect instead of returning a String but it works in both ways)</p> <pre><code>public void goToLink() throws IOException { // get WebDAV file and save temporarily byte[] b = new Helper().getWebDavFileManually(customerId, fileName); String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") + fileName; File f = new File(path); try { FileOutputStream fos = new FileOutputStream(f); fos.write(b); link = "http://someurl/somepage/" + fileName; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // use link ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); ec.redirect(link); } </code></pre> <p>This final approach worked fine even on my android device but I <strong>do not want</strong> to go this route if I can avoid it because the file gets streamed from a WebDAV an I'd have to save each file to the server. This would produce more IO load and would force me to manually clean-up.</p></li> </ul> <p>The methods <code>Helper().getWebDavFile</code> and <code>Helper().getWebDavFileManually</code> either return a <em>DefaultStreamedConten</em> used by PrimeFaces or a <em>byte[]</em> for my own approach.</p> <p><strong>What I know so far:</strong></p> <p>Unfortunately not the solution for my problem :).<br> After many hours of using google I found out that there is the possibility of a <em>double-http-post-request</em>. This would cause androids internal download manager (used in case of broken file download) to send an additional post-request in which the state gets lost.</p> <p>As described in <a href="http://digiblog.de/2011/04/19/android-and-the-download-file-headers/" rel="nofollow noreferrer">this blog</a> (see section <em>GET, POST, REST [UPDATE 20120208]</em>) there is someone facing the same problem. I've tried all approaches mentioned on this blog but didn't succeed.<br> On this <a href="http://androidforums.com/application-development/256987-android-phone-download-issue-asp-net-website.html#post3687287" rel="nofollow noreferrer">forum</a> someone analyzed the same behavior with <em>WireShark</em> and got nearly to the same conclusion.<br> I didn't found any more ressources so I'm stuck at this.</p> <p>I've also posted on the PrimeFaces <a href="http://forum.primefaces.org/viewtopic.php?f=8&amp;t=19055" rel="nofollow noreferrer">forum</a> just to make sure that there aren't any known issues regarding the <code>&lt;p:fileDownload&gt;</code> component.</p> <p><strong>What I would like to know:</strong></p> <p>Am I missing something?<br> Is there any possibility to download a streamed file from an JSF (http-post operating) webpage on an android device?</p> <p>Any help/suggestion/information would be appreciated!<br> <strong>Thanks in advance!</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload