[Tobago] Problem with PDF download and Ajax
Hi, I've a page with a link to download a PDF file. The PDF download itself works without a problem. But after a download Ajax requests don't work anymore, for example a popup or a tabGroup with reloadTab. Here's a small testcase for this behavior. The page: <%@ taglib uri="http://myfaces.apache.org/tobago/component"; prefix="tc" %><%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page pageEncoding="UTF-8" %> The java code for the PDF download: public void showPDFActionListener(ActionEvent e) { String filename = (String)e.getComponent().getAttributes().get("file"); sendFile(attachmentPath, filename, "application/pdf"); } protected void sendFile(String filepath, String filename, String mimeType) { File file = new File(filepath, filename); if (file.exists() && file.isFile()) { try { InputStream in = new FileInputStream(file); FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse(); response.setContentType(mimeType); response.setContentLength(in.available()); response.setHeader("Content-disposition", " attachment;filename=\"" + filename + "\""); ServletOutputStream out = response.getOutputStream(); byte[] buffer = new byte[0x1]; while (in.read(buffer) > 0) { out.write(buffer); } in.close(); out.close(); StateManager stateManager = (StateManager)facesContext.getApplication().getStateManager(); stateManager.saveSerializedView(facesContext); facesContext.responseComplete(); } catch (Exception x) { getLog().error("Error writing output", x); } } else { getLog().error("File " + file.getAbsolutePath() + " not found"); } } I think there's a problem with Ajax when a page submit has no result. Regards Helmut
Re: [Tobago] Problem with PDF download and Ajax
Hello Helmut, just solved see: http://issues.apache.org/jira/browse/TOBAGO-303 Should be available with the next nightly build Regards Bernd H. Swaczinna wrote: Hi, I've a page with a link to download a PDF file. The PDF download itself works without a problem. But after a download Ajax requests don't work anymore, for example a popup or a tabGroup with reloadTab. Here's a small testcase for this behavior. The page: <%@ taglib uri="http://myfaces.apache.org/tobago/component"; prefix="tc" %><%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ page pageEncoding="UTF-8" %> > rows="fixed;fixed"/> label="Close"> The java code for the PDF download: public void showPDFActionListener(ActionEvent e) { String filename = (String)e.getComponent().getAttributes().get("file"); sendFile(attachmentPath, filename, "application/pdf"); } protected void sendFile(String filepath, String filename, String mimeType) { File file = new File(filepath, filename); if (file.exists() && file.isFile()) { try { InputStream in = new FileInputStream(file); FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse)facesContext.getExternalContext().getResponse(); response.setContentType(mimeType); response.setContentLength(in.available()); response.setHeader("Content-disposition", " attachment;filename=\"" + filename + "\""); ServletOutputStream out = response.getOutputStream(); byte[] buffer = new byte[0x1]; while (in.read(buffer) > 0) { out.write(buffer); } in.close(); out.close(); StateManager stateManager = (StateManager)facesContext.getApplication().getStateManager(); stateManager.saveSerializedView(facesContext); facesContext.responseComplete(); } catch (Exception x) { getLog().error("Error writing output", x); } } else { getLog().error("File " + file.getAbsolutePath() + " not found"); } } I think there's a problem with Ajax when a page submit has no result. Regards Helmut
Re: [Tobago] Problem with PDF download and Ajax
Hi Helmut, As per ur code, u give the location of a PDF doc on the server. Which when referred then that would be downloaded. But what if a pdf doc is generated within a javacode, say with Jasper Reports. How can i pass that pdf doc generated in runtime to the jsp page and display that ? Thnx in Advance, Regards, Madan - Original Message From: Bernd Bohmann <[EMAIL PROTECTED]> To: MyFaces Discussion Sent: Tuesday, 27 February, 2007 4:25:31 AM Subject: Re: [Tobago] Problem with PDF download and Ajax Hello Helmut, just solved see: http://issues.apache.org/jira/browse/TOBAGO-303 Should be available with the next nightly build Regards Bernd H. Swaczinna wrote: > Hi, > > I've a page with a link to download a PDF file. The PDF download itself > works without a problem. But after a download Ajax requests don't work > anymore, for example a popup or a tabGroup with reloadTab. > > Here's a small testcase for this behavior. > > The page: > > <%@ taglib uri="http://myfaces.apache.org/tobago/component";; prefix="tc" > %><%@ taglib uri="http://java.sun.com/jsf/core";; prefix="f" > %><%@ page contentType="text/html;charset=UTF-8" language="java" > %><%@ page pageEncoding="UTF-8" > %> > > > >rows="fixed;fixed"/> > >label="PDF" > immediate="true" > transition="false" > actionListener="#{anlagenController.showPDFActionListener}"> > > >label="Ajax"> > > > >label="Close"> > > > > > > > > > > The java code for the PDF download: > > public void showPDFActionListener(ActionEvent e) { > String filename = (String)e.getComponent().getAttributes().get("file"); > sendFile(attachmentPath, filename, "application/pdf"); > } > > protected void sendFile(String filepath, String filename, String mimeType) { > File file = new File(filepath, filename); > if (file.exists() && file.isFile()) { > try { > InputStream in = new FileInputStream(file); > FacesContext facesContext = FacesContext.getCurrentInstance(); > HttpServletResponse response = > > (HttpServletResponse)facesContext.getExternalContext().getResponse(); > response.setContentType(mimeType); > response.setContentLength(in.available()); > response.setHeader("Content-disposition", > " attachment;filename=\"" + filename + "\""); > ServletOutputStream out = response.getOutputStream(); > byte[] buffer = new byte[0x1]; > while (in.read(buffer) > 0) { > out.write(buffer); > } > in.close(); > out.close(); > StateManager stateManager = > (StateManager)facesContext.getApplication().getStateManager(); > stateManager.saveSerializedView(facesContext); > facesContext.responseComplete(); > } catch (Exception x) { > getLog().error("Error writing output", x); > } > } else { > getLog().error("File " + file.getAbsolutePath() + " not found"); > } > } > > I think there's a problem with Ajax when a page submit has no result. > > Regards > Helmut > __ Yahoo! India Answers: Share what you know. Learn something new http://in.answers.yahoo.com/
RE: Re: [Tobago] Problem with PDF download and Ajax
Hi Madan,here's a (stripped) example I use to generate print files within java code: label="Print" immediate="true" transition="false" action=""/> public void printAction() { String pathname = System.getProperty("java.io.tmpdir"); String filename = "test.pdf"; File pdfFile = new File(pathname, filename); try { PrintStream out = new PrintStream(pdfFile); // Write the PDF content out.println(...); out.close(); sendFile(pathname, filename, "application/pdf"); } catch (IOException x) { LOG.error("Can't create PDF file", x); } }RegardsHelmut Hi Helmut,As per ur code, u give the location of a PDF doc on the server.Which when referred then that would be downloaded.But what if a pdf doc is generated within a javacode, say with Jasper Reports.How can i pass that pdf doc generated in runtime to the jsp page and display that ?Thnx in Advance,Regards,Madan - Original Message From: Bernd Bohmann <[EMAIL PROTECTED]>To: MyFaces Discussion Sent: Tuesday, 27 February, 2007 4:25:31 AMSubject: Re: [Tobago] Problem with PDF download and AjaxHello Helmut,just solved see:http://issues.apache.org/jira/browse/TOBAGO-303Should be available with the next nightly buildRegardsBerndH. Swaczinna wrote:> Hi,> > I've a page with a link to download a PDF file. The PDF download itself> works without a problem. But after a download Ajax requests don't work> anymore, for example a popup or a tabGroup with reloadTab.> > Here's a small testcase for this behavior.> > The page:> > <%@ taglib uri="http://myfaces.apache.org/tobago/component"; prefix="tc" > %><%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f" > %><%@ page contentType="text/html;charset=UTF-8" language="java" > %><%@ page pageEncoding="UTF-8" > %>> >> > > > rows="fixed;fixed"/>> > > label="PDF"> immediate="true"> transition="false"> actionListener="#{anlagenController.showPDFActionListener}">> > > > label="Ajax">> > > > > label="Close">> > > > > > > > > > The java code for the PDF download:> > public void showPDFActionListener(ActionEvent e) {> String filename = (String)e.getComponent().getAttributes().get("file");> sendFile(attachmentPath, filename, "application/pdf");> }> > protected void sendFile(String filepath, String filename, String mimeType) {> File file = new File(filepath, filename);> if (file.exists() && file.isFile()) {> try {> InputStream in = new FileInputStream(file);> FacesContext facesContext = FacesContext.getCurrentInstance();> HttpServletResponse response = > (HttpServletResponse)facesContext.getExternalContext().getResponse();> response.setContentType(mimeType);> response.setContentLength(in.available());> response.setHeader("Content-disposition", > " attachment;filename=\"" + filename + "\"");> ServletOutputStream out = response.getOutputStream();> byte[] buffer = new byte[0x1];> while (in.read(buffer) > 0) {> out.write(buffer);> }> in.close();> out.close();> StateManager stateManager = (StateManager)facesContext.getApplication().getStateManager();> stateManager.saveSerializedView(facesContext); > facesContext.responseComplete();> } catch (Exception x) {> getLog().error("Error writing output", x);> }> } else {> getLog().error("File " + file.getAbsolutePath() + " not found");> }> }> > I think there's a problem with Ajax when a page submit has no result.> > Regards> Helmut> Here’s a new way to find what you're looking for - Yahoo! Answers