Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 如何使用Ajax检测JSF2.0中的文件上传完成事件_Jsf 2 - Fatal编程技术网

Jsf 2 如何使用Ajax检测JSF2.0中的文件上传完成事件

Jsf 2 如何使用Ajax检测JSF2.0中的文件上传完成事件,jsf-2,Jsf 2,希望你们都会好起来。实际上我想把图像文件从系统上传到数据库。但我希望当用户上传文件时,文件不会直接进入数据库,而是在选择文件后。文件可能会保存到某个临时位置,所以当用户单击“保存”按钮时,我会将所有图像移动到数据库。我正在使用JSF2.0和PrimeFaces。我在某人的博客上找到了代码。代码的作用是在上传文件后,将其转换为byte[]数组。我将那个字节数组保存在一个列表中,所以按save按钮,我从列表中获取图像 这是密码 private StreamedContent image; publ

希望你们都会好起来。实际上我想把图像文件从系统上传到数据库。但我希望当用户上传文件时,文件不会直接进入数据库,而是在选择文件后。文件可能会保存到某个临时位置,所以当用户单击“保存”按钮时,我会将所有图像移动到数据库。我正在使用JSF2.0和PrimeFaces。我在某人的博客上找到了代码。代码的作用是在上传文件后,将其转换为byte[]数组。我将那个字节数组保存在一个列表中,所以按save按钮,我从列表中获取图像

这是密码

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>

所以当用户点击save按钮时,我会将所有图像移动到数据库中

那就相应地实施吧

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

嗨,谢谢,但是Ajax部分呢。如何检测文件上传完成事件?嗯,你的意思是说不需要使用“代码”“代码”。我还用actionListener完成了文件上传部分。是更好还是没有必要使用actionListener?我已经按以下方式完成了[code][/code]谢谢这不起作用[code][/code]有什么技巧可以在完成后运行吗?谢谢,这样,使用
update
属性,就像您已经用于消息一样。嗨,我通过创建ImagePath解决了我的isseu问题。除了获取图像,我使用了如下代码[code][/code],然后在bean中使用如下代码[code]私有字符串imagePath=“/resources/images/Untitled.jpg”;私有字符串imagePath1=“/resources/images”+“/”;[/code]然后我使用单击的图像创建了路径谢谢
public void save() {
    // Move all images to database.
}