Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6772771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:35:17+00:00 2026-05-26T15:35:17+00:00

Hope you all will be fine. Actually i want to upload image files from

  • 0

Hope you all will be fine. Actually i want to upload image files from system to database. But i want that when user upload files then file didn’t go to database directly but after selecting files .Files may save to some temporery location, so when user click on save button then i move all the images to database. I am using JSF 2.0 and PrimeFaces. I found code on someone blog. What code does is that after uploading files , it converted it to byte[] array. I save that byte Array in a list so on save button i get images from the List.

Here is the code

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()

Here is my .html file

<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>

Now i want that when image is completely uploaded, the image is also shown to the panel grid 3rd column. For this i am trying to use Ajax. But i don’t know what event should i use. So please can any one tell me the event name and also i want to ask for execute i should use “@this” ? .Also tell me that is my approach is right that to save binary image in a List so that when user click on save button then i retreive all the images and send them to database. Also using Ajax is right idea here for my purpose?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:35:17+00:00Added an answer on May 26, 2026 at 3:35 pm

    so when user click on save button then i move all the images to database

    Then just implement that accordingly?

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

    with

    public void save() {
        // Move all images to database.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friday afternoon and all but hope that different timezones will help me with this...
Hope you all will be fine. The scenerio is that i have a page
Hope you all will be fine. The scenerio is I have a form on
I don't know PHP all that well so I hope I will be showing
hope all is swell! I was hoping I had cracked this problem but it
Hope you all are fine and rocking your code editors. My question is how
I hope to list all files in document directory using codes below for(NSString *path
I hope you are all fine. I would like to ask the expert php
I hope this is a simple problem. I have a seekbar that I want
first of all, I hope that I ask the question in the right context

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.