No 'save as' fileDialog pops up using commandLink
janvandam Dec 1, 2009 6:09 PMMy problem is to download a file rom the server. When using a commandLink like this:
<a:commandLink id="download" value="Download" action="#{outputFileDialogAction.initDownload(outputFile)}" oncomplete="Richfaces.showModalPanel('dialogActionPanel');" propagation="none" rendered="#{empty from}" reRender="dialogActionPanel" limitToList="true"/>
my backing bean is called and the selected outputFile from my dataTable is handed over to the backing bean. However the code from my backing bean:
public void executeDownload(OutputFile outputFile) { FacesContext facesContext = FacesContext.getCurrentInstance(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter writer = new PrintWriter(baos); writer.write("content", 0, 7); writer.write("\n"); writer.flush(); writer.close(); HttpServletResponse response = ((HttpServletResponse)facesContext.getExternalContext().getResponse()); response.setContentType("text/plain"); response.addHeader("Content-disposition", "attachment; filename=\"" + "filename" +"\""); ServletOutputStream sos; try { sos = response.getOutputStream(); sos.write(baos.toByteArray()); sos.flush(); sos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } facesContext.responseComplete(); }
... does not result in a 'save as' dialog as expected; instead the data ('content') is written to the browser.
If on the other hand, I use an s:link:
<s:link view="/OutputFileList.xhtml" action="#{outputFileDialogActions.initDownload(outputFile)}" value="Download" propagation="none" id="outputFileList2" rendered="#{empty from}"> <s:conversationPropagation type="begin"/> </s:link>
I do get a 'save as' popup as desired, however the outputFile parameter is null, so I am not able to retrieve to data I want to download.
Can somebody help me?