0

I am uploading a file in jsf and I would like to show user progress on the output label. This is what I have tried so far but its only getting updated after the upload is complete.

<h:form enctype="multipart/form-data" id="form_upload">

                             <p:panelGrid columns="2">
                                         <p:outputLabel value="Total Excel count"/>
                                          <h:outputLabel value="#{order.total_excel_count}"/>
                                          <h:outputLabel value="Total Uploaded"/>
                                          <h:outputLabel value="#{order.uploaded}"/>

                                           <br/>
                                           <br/>
                                           <br/>
                                    </p:panelGrid>
                            <p:messages closable="true" showDetail="true"/>
                            <p:fileUpload mode="simple" accept=".csv" value="#{order.uploadedFile}" />
                            <p:commandButton update="form_upload" value="Upload" action="#{order.upload}" ajax="false" id="uploader"/>
    </h:form>

and my back bean

@ManagedBean(name = "order")
@ViewScoped
public class Upload {
         int uploaded,total_excel_count;//getter setter
    private UploadedFile uploadedFile;//getter setter
   public void upload() {


        try {
            InputStream in = uploadedFile.getInputstream();
            Scanner scanner = new Scanner(in);
            List<String> s = new ArrayList();
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                s.add(line);
                uploaded++;
                 doInsert(s);//method to do insert in the db

            }
            scanner.close();
            total_excel_count=s.size();

            setExist(total_excel_count-getUploaded());
        } catch (Exception e) {
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "!Some Error Occured", "Card Upload"));

            e.printStackTrace();
        }

    }

        }
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Effectively you are asking: "How can I drive a car without driving a car" – Kukeltje Mar 17 '20 at 21:49
  • I dont get you. Is it possible to achieve that – Morgan Denis Mar 18 '20 at 02:36
  • @Kukeltje what do u mean by that? –  Mar 18 '20 at 05:07
  • Updating parts of / in a page during processing of an upload requires ajax and you don't (want to?) use ajax. Hence the "You want to drive a car without driving car" – Kukeltje Mar 18 '20 at 08:06
  • @Kukeltje is it possible to upload a file in jsf using ajax? –  Mar 18 '20 at 08:34
  • Tried using a searchengine? PrimeFaces showcase? PrimeFaces documentation? Answer is yes in modern browsers and modern jsf/primefaces – Kukeltje Mar 18 '20 at 08:59
  • @Kukeltje I tried following this tutorial but my counter output label is not getting updated as the upload progress https://www.logicbig.com/tutorials/misc/primefaces/file-upload.html –  Mar 18 '20 at 09:07
  • The has its own built-in progress bar when used in advanced mode. – BalusC Mar 18 '20 at 14:00
  • @BalusC kindly assist me on that –  Mar 18 '20 at 15:56
  • Just use the example snippet from their own documentation/showcase. – BalusC Mar 18 '20 at 16:57
  • @BalusC my case is different. mine is a user is supposed to select excel file then managed bean read the csv and insert it in the db. I need to show the user the progress of insert because the csv can contains a million records –  Mar 18 '20 at 17:48
  • Oh. That's not "upload progress" at all. Fix your question accordingly. It is not related to uploading files. See duplicate for the answer. – BalusC Mar 18 '20 at 23:02

1 Answers1

1

You can't show progress of an upload in a page when not using an 'ajax' based upload

Kukeltje
  • 12,223
  • 4
  • 24
  • 47