Note that there are some explanatory texts on larger screens.

plurals
  1. POHandle varbinary file to download, upload, and display? Primefaces 3.4 - fileupload - filedownload - media
    primarykey
    data
    text
    <p>I use Glassfish 3.1.2.2, Sql Server 2005, PF 3.4 and Mojarra 2.1.6. Ide Eclipse Juno.</p> <p>I use stored procedure, and add jar commons-io.1.4 and commons-fileupload.1.2.1</p> <p><strong>In database, we save all file (pdf, doc, xls, etc.) in varbinary.</strong> I have these doubts:</p> <ol> <li>How I can upload any <strong>file (pdf, doc, xls, etc.)</strong> with <strong>p:fileupload</strong> and save as <strong>varbinary</strong> file in the <strong>database</strong>?</li> <li>How I can download any <strong>varbinary</strong> file of <strong>database</strong> with <strong>p:FileDownload</strong>?</li> <li>How I can show pdf with <strong>p:media</strong>, recovering files <strong>varbinary</strong> from the <strong>database</strong>?</li> </ol> <p><strong>ONE QUESTION</strong> </p> <p>How I can upload any file (pdf, doc, xls, etc.) with p: fileupload and save them to the database?</p> <p>I undestood the p:fileupload, since documentation. I use this method, handlerFileUpload, for upload a file. Firsts get the inputScream of the event</p> <pre><code>&lt;p:fileUpload fileUploadListener="#{fileBean.handleFileUpload}" /&gt; public class FileBean { public void handleFileUpload(FileUploadEvent event) { UploadedFile file = event.getFile(); //application code } } </code></pre> <p>I used file.getInputStream and sent the stored procedure a BinaryStream setBinaryStream(4, inputStream, (int)inputStream.toString().getBytes().length);</p> <p>This is good??</p> <p><strong>TWO QUESTION</strong></p> <p>How I can download any <strong>varbinary</strong> file with <strong>p:FileDownload</strong>?</p> <p>I recover a varbinary since database with getBytes. Remember I use the stored procedure.</p> <p>This method is in a class with request scope.</p> <pre><code> public byte[] ArchivoBin(int CDTPEnlaceImagen, int iddoc) { Context(); Null_Rs_and_Proc(); Connection(0); PA(2, 51); [b]byte[] file = null;[/b] try { setProc(getCon().prepareCall(getCall())); getProc().setInt(1, CDTPEnlaceImagen); getProc().setInt(2, iddoc); setRs(getProc().executeQuery()); getRs().next(); [b]file = getRs().getBytes(1);[/b] System.out.println(file); } catch(SQLException e) { System.out.println("Exception ArchivoBin"); System.out.println(e); } CloseConnections(); return file;} </code></pre> <p>But, when i get the file in bytes, i convert a bytes to inputscream, and then inputscream to DefaultStreamedContent.</p> <p>This part is a class with request scope.</p> <pre><code>byte[] bytes = null; bytes = getRecuperarDatos().ArchivoBin( 1, idarchivo); //Archivo is a Session Scope getArchivo().setFile(new DefaultStreamedContent( [b]new ByteArrayInputStream(bytes)[/b], "application/pdf", nomArchivo)); </code></pre> <p>The <strong>f:download</strong> not get the file correct, It is corrupt.</p> <pre><code>&lt;h:commandButton value="Download"&gt; [b]&lt;p:fileDownload value="#{archivo.file}"[/b] /&gt; &lt;/h:commandButton&gt; </code></pre> <p>I don't know why this bad</p> <p><strong>THREE QUESTION</strong> </p> <p>How I can show pdf with <strong>p:media</strong>, recovering <strong>varbinary files from the database</strong>?</p> <p>This is similar to TWO QUESTION, p:media say "An error has occurred while loading the PDF document"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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