I am trying to display PDF file using p: media , but Nothing shows up (at the PDF Previewer dialog box) This code works to display directly on the browser and also works to download the file. When I click on the preview link, then there is no error in the code below: This is a backing bean: If you need more information, do not hesitate to ask me. Thank you. The source code for the primetime MediaRenderer.java class indicates that the value of P is: Media either An example of a streamed content can be (as you have it) or string-based URLs. The problem I see from my GetFile is the selected method that sets Header / Content Type etc. on HttpServletResponse. This is problematic for two reasons: 1) If you ever need to do things like this, then you should use methods of resource response (part of portlet API) 2) Depending on when the GetFileSelected method is being called, you can try to change the content type of an Ajax request. Primetime Javascript in the client is expecting a partial response to JSF, but you can convert it to binary app / PDF. I think the best way to handle it is to avoid returning an example of streaming content, and instead specify the value attribute like this one URL: and then a custom resource and resource handler such as jsf2-export-pdf-portlet.
& lt; H: form enctype = "multipart / form-data" & gt; & Lt; P: datatable id = "files" var = "file" value = "# {viewBacking.getFileList ()}" & gt; & Lt; P: column headerText = "file" & gt; & Lt; H: output text value = "# {file.name}" /> & Lt; / P: column & gt; & Lt; P: column style = "width: 5%" & gt; & Lt; P: commandLink id = "downloadLink" value = "download" title = "download file" ajax = "false" actionListener = "# {viewBacking.getFile selected (file, 1)}" & gt; & Lt; P: file download download = "# {viewbacking.file}" /> & Lt; / P: commandLink & gt; & Lt; / P: column & gt; & Lt; P: column style = "width: 5%" & gt; & Lt; P: commandLink id = "previewlink" value = "preview" title = "preview file" ajax = "false" actionListener = "# {viewBacking.getFile selected (file, 2)}" onclick = "dialog.pdf.show () "& Gt; & Lt; / P: commandLink & gt; & Lt; / P: column & gt; & Lt; / P: DataTable & gt; & Lt; P: dialog header = "images" widget array = "dialog pdf" model = "true" dragable = "fottal" rhesgele = "fottal" width = "1040" height = "500" & gt; & Lt; P: media value = "# {viewbacking.file}" width = "100%" height = "300px" & gt; & Lt; / P: Media & gt; & Lt; / P: Dialog & gt; & Lt; / H: form & gt;
public class viewbacking {personalized streamed content file; Public streamed content getFile () {return file; } Public streamed content LateFile throws exceptions (Last stream content doctor, int mode) {// Mode: 1-download, try 2-preview {file local file = new file (getPath (doc.getName ())) ; FileInputStream fis = New FileInputStream (local file); // If the mode preview and extension & lt; & Gt; Switch to `pdf`,` pdf` if (mode == 2 & amp; (Doc.getName (). Substrings (doc.getName). LastIndexOf (".") + 1)). Matches ("PDF ")) {Localfile = DocumentConversionUtil.convert (doc.getName (), fis, doc.getName (). Substring (doc.getName (). LastIndexOf (". ") + 1)," PDF "); Fis = New FileInputStream (localfile.getPath ()); } If (localfile.exists ()) {try} portletResponse portletResponse = (PortletResponse) FacesContext.getCurrentInstance (). GetExternalContext (). GetResponse (); HttpServletResponse res = PortalUtil.getHttpServletResponse (portletResponse); If (mode == 1) res.setHeader ("content-displacement", "attachment; filename =" "+ doc.getName () +" \ ""); Else if (mode == 2) res.setHeader ("content-displacement", "inline; filename = \" "+ doc.getName (). Substrings (0, doc.getName (). LastIndexOf (". ")) + ".pdf \" "); Res.setHeader ("Content-Transfer-Encoding", "Binary"); Res.setContentType (getMimeType (localfile.getName (). Nickname (localfile.getName (). LastIndexOf (".") + 1))); Res.flushBuffer (); OutputStream Out = res.getOutputStream (); Byte [] buffer = new byte [4096]; Int bytes read; While ((Bytes read = Effice Reid (buffer))! = -1) {Out. Written (buffer, 0, bytes read); Buffer = new byte [4096]; } File = new DefaultStreamedContent (fis, "application / pdf", "file.pdf"); // -> I tried to add this line to end the stream. Stream (out) Flush (); Out.close (); Out = null; } Hold (IOException e) {e.printStackTrace (); } Finally {fis.close (); Fis = null; System.gc (); }}} Hold (Exception pre) {ex.printStackTrace (); } Return tap; }}
No comments:
Post a Comment