2011-11-01 55 views
1

希望你们一切都好。其实我想上传图像文件从系统到数据库。但我想,当用户上传文件,然后文件没有直接进入数据库,但选择文件后。文件可能会保存到一些temporery的位置,所以当用户点击保存按钮,然后我将所有的图像移动到数据库。我正在使用JSF 2.0和PrimeFaces。我在某人的博客上找到了代码。代码的作用是在上传文件后将其转换为byte []数组。我将这个字节数组保存在一个列表中,所以保存按钮我从列表中获取图像。如何使用Ajax检测JSF2.0中的文件上传完成事件

下面是代码

private StreamedContent image; 

public StreamedContent getImage() { 
    return image; 
} 

public void setImage(StreamedContent image) { 
    this.image = image; 
} 

public String imageUpload(FileUploadEvent event) { 

    try { 

     // Convert file from input stream to bytes 
     byte[] byteArray = InputStreamToByte(event.getFile().getInputstream()); 

     /** 
     * Add images to list so we can retrieve them when user click on save button 
     */ 
     boolean add = images.add(new Images(byteArray)); 


     /** 
     * Class.forName("org.postgresql.Driver"); 
      String url = "jdbc:postgresql://x.x.x.x:5432/MYDB"; 
      Connection oConnection = DriverManager.getConnection(url, "username", "password"); 
      System.out.println("Sucessfully connected to Postgres Database"); 
     * 
     * byte[] bytes = bos.toByteArray(); 
      String sql = "INSERT INTO my_table (byte_array) VALUES (?)"; 
      statement = oConnection.prepareStatement(sql); 
      statement.setBytes(1, bytes); 
      statement.executeUpdate(); 
      System.out.println("Image inserted into database!"); 
     */ 

     //byteArray used to store picture into database 
     InputStream ist = new ByteArrayInputStream(byteArray); 

     /* 
     * StreamedContent Object used to show the picture in jsf pages. We need to convert 
     * again bytearray to InputStream 
     */ 
     image = new DefaultStreamedContent(ist, "image/jpg"); 

     FacesMessage msg = new FacesMessage("Succesful picture is uploaded."); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 

     //we dont need to use faces-config to navigate in jsf 2 
     return null ; 

    } catch (Exception e) { 

     FacesMessage msg = new FacesMessage("Exception happen"); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 

     e.printStackTrace(); 

     return null; 

    } 

} //end of imageUpload() 

这里是我的.html文件

<h:panelGrid width="30%" columns="3"> 

    <h:outputText value="Map to upload" /> 
    <p:fileUpload id="uploadFile" 
        description="Image" 
        update="messages" 
        allowTypes="*.jpg;*.png;*.gif;*.jpeg;" 
        auto="true" 
        fileUploadListener=#{cityDetail.imageUpload} > 

     <f:ajax event="????" execute="???" render="uploadedInage" /> 

    </p:fileUpload> 

    <p:graphicImage id="uploadedImage" 
        value="#{cityDetail.image}" 


</h:panelGrid> 

现在我想的是,当图像被完全上传,图像也显示面板电网第三列。为此,我尝试使用Ajax。但我不知道我应该使用什么事件。所以请任何人告诉我事件名称,我也想要求执行我应该使用“@this”? 。也告诉我,我的方法是正确的,将二进制图像保存到列表中,以便当用户单击保存按钮时,然后我检索所有图像并将它们发送到数据库。为了我的目的,使用Ajax也是正确的想法吗?

感谢

回答

0

所以当用户点击Save按钮,然后我把所有的图像数据库

然后,只需执行该相应?

<p:commandButton value="save" action="#{cityDetail.save}" /> 

public void save() { 
    // Move all images to database. 
} 
+0

您好感谢,但对于Ajax的一部分。我如何检测文件上传完成事件? – Basit

+0

你已经在'fileUploadListener'中使用它了。 – BalusC

+0

嗯,你的意思是说没有必要使用'代码''code'。另外我还用actionListener完成了文件上传部分。更好还是不需要使用actionListener?我按照以下方式完成[code] [/ code]谢谢 – Basit