0

I use primefaces and I want to download a file with different formats(pdf, jpg, png, ) from database but I don't succed to realise this I have seen a lot of topic like this but their methods don't work for me here is the html :

<ui:repeat value="#{histCommController.selectedCommande.listFichiers}" var="jjjjj">
                            <tr>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.nom}" />
                                </td>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.taille}" />
                                </td>
                                <td>
                                    <p:commandButton id="downloadLink" value="Download" ajax="false"   
                                                     icon="ui-icon-arrowthichk-s">  
                                        <p:fileDownload value="#{jjjjj.convertFichieru}" /> 
                                    </p:commandButton>
                                </td>
                            </tr>



                        </ui:repeat>

and here is the java :

 public StreamedContent convertFichier(byte[] bytes) {
    InputStream is = new ByteArrayInputStream(bytes);
    System.out.println("size file : "+bytes.length);
    StreamedContent image = new DefaultStreamedContent(is);
    System.out.println("dans le convertisseur : "+image.getContentType());
    return image;
}

always, image.getContentType() return null and bytes.length not null

do you have any idea

thank you


I just know the problem, I put the download link in dialog box, because when i make a test outside the dialog it works here is the test that it works :

 <h:form>

            <p:commandLink id="downloadLink" value="Download" ajax="false">  
                <p:fileDownload value="#{histCommController.test}" />  
            </p:commandLink> 
            </h:form>

and here is the test inside the dialog :

<p:dialog header="Car Detail" widgetVar="carDialog" resizable="false" id="carDlg"  
                          showEffect="fade" hideEffect="explode" modal="true">  

                    <table id="gradient-style" >
                        <tr style="border: hidden;">
                            <th>
                                <h:outputLabel value="Model:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.id}" />
                            </td>                                       
                        </tr>
                        <tr style="border: hidden;">
                            <th>
                                <h:outputLabel value="Year:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.etat}" />
                            </td>                                       
                        </tr>
                        <tr style="border: hidden;">
                            <th>
                                <h:outputLabel value="Manufacturer:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.dateEnvoi}" />
                            </td>                                       
                        </tr>
                        <tr style="border: hidden;">
                            <th>

                                <h:outputLabel value="Color:" />
                            </th>
                            <td>
                                <h:outputLabel style="font-weight: bold;" value="#{histCommController.selectedCommande.dateLivraisonRecommande}" />
                            </td>                                       
                        </tr>
                    </table>
                    <table id="gradient-style" >
                        <th>Nom Fichier</th><th>Taille</th><th>Télécharger</th>
                        <ui:repeat value="#{histCommController.selectedCommande.listFichiers}" var="jjjjj">
                            <tr>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.nom}" />
                                </td>
                                <td>
                                    <h:outputLabel style="font-weight: bold;" value="#{jjjjj.taille}" />
                                </td>
                                <td>
                                    <p:commandLink id="downloadLink" value="Download" ajax="false">  
                                        <p:fileDownload value="#{histCommController.test}" />  
                                    </p:commandLink>
                                </td>
                            </tr>



                        </ui:repeat>

                    </table>


                </p:dialog>  

this latter doesn't work

do you any idea how to make working the download link inside the dialog

thank you in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
begiPass
  • 2,124
  • 6
  • 36
  • 57
  • You didn't set the content type and therefore it is null. You can set the content type in the constructor of `DefaultStreamedContent`. – siebz0r Aug 11 '12 at 13:19
  • I set the content type and after image.getContentType() return the value that I put, but the file don't download, and in the table in the database, the file has more than 20000 kb (I save it in longblob in mysql), can you detect me the problem, thanks – begiPass Aug 12 '12 at 09:47

1 Answers1

0

You should set the content type yourself. Take a look at the constructors of DefaultStreamedContent. So instead of

StreamedContent image = new DefaultStreamedContent(is);

You should write

StreamedContent image = new DefaultStreamedContent(is, "image/jpeg", "fileName.jpg");

Make sure you use the proper content type, and extension for filename.

Adam
  • 5,045
  • 1
  • 25
  • 31