1. #1
    Ext GWT Premium Member
    Join Date
    Sep 2011
    Posts
    14
    Vote Rating
    0
    B.Oury is on a distinguished road

      0  

    Question Unanswered: Upload Max Size File and progress bar

    Unanswered: Upload Max Size File and progress bar


    Hi,

    I use a FormPanel to upload a file and cause I can't fix the maximum size of the file in the client, I calculate it in the servlet and throw a new javax.xml.ws.http.HTTPException(400) if the file's size is larger than 5000000.

    The problem is that nothing happens.

    There's some code.

    In the client:

    Code:
    public class UploadPanel extends LayoutContainer {
      private final Messages messages = GWT.create(Messages.class);
      private MyFileUploadField myFileUploadField;
      private HiddenField<Long> idTask = new HiddenField<Long>();
      private HiddenField<Long> idPatient = new HiddenField<Long>();
    
      @Override
      protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setStyleAttribute("margin", "10px");
        final FormPanel panel = new FormPanel();
        panel.setHeading(messages.ajouterUnFichier());
        panel.setFrame(true);
        panel.setAction("uploadFile");
        panel.setEncoding(Encoding.MULTIPART);
        panel.setMethod(Method.POST);
        panel.setButtonAlign(HorizontalAlignment.CENTER);
        panel.setWidth(350);
    
        myFileUploadField = new MyFileUploadField();
        myFileUploadField.setAllowBlank(false);
        myFileUploadField.setShim(true);
        myFileUploadField.setName("uploadedfile");
        myFileUploadField.setFieldLabel(messages.fichier());
        panel.add(myFileUploadField);
    
        Button btn = new Button(messages.Save());
        btn.addSelectionListener(new SelectionListener<ButtonEvent>() {
    
          @Override
          public void componentSelected(ButtonEvent ce) {
            if (!panel.isValid()) {
              return;
            }
            idPatient.setValue(5432L);
            idTask.setValue(6789L);
                panel.submit();
            }
        });
    
        idPatient.setName("idPatient");
        panel.add(idPatient);
        idTask.setName("idTask");
        panel.add(idTask);
        panel.addButton(btn);
    
        add(panel);
      }
    }
    In the Servlet:

    Code:
    public class UploadServlet extends HttpServlet {
      private File tmpDir;
      private File destinationDir;
    
      public void init(ServletConfig config) throws ServletException {
        super.init(config);
      }
    
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");
    
        DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
        fileItemFactory.setSizeThreshold(1 * 1024 * 1024); //1 MB
        fileItemFactory.setRepository(tmpDir);
    
        ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
        uploadHandler.setSizeMax(5000000);
        File file = null;
    
        FileItem item = null;
        try {
    
          List items = uploadHandler.parseRequest(request);
          Iterator itr = items.iterator();
          while (itr.hasNext()) {
            item = (FileItem) itr.next();
    
            if (item.isFormField()) {
              out.println("File Name = " + item.getFieldName() + ", Value = " + item.getString());
            } else {
              file = new File(destinationDir, item.getName());
              file.setWritable(true);
              item.write(file);
    
              OmniUser connectedUser = (OmniUser) request.getSession().getAttribute("USER");
    
              String mimeType = this.getServletContext().getMimeType(file.getPath());
              //TODO something
            }
          }
        } catch (FileUploadException ex) {
          log("Error encountered while parsing the request", ex);
        } catch (Exception ex) {
          if (ex instanceof HTTPException) {
            log("Fichier trop volumineux", ex);
            throw new HTTPException(400);
          } else {
            log("Error encountered while uploading file", ex);
          }
        } finally {
          if (out != null) {
            out.close();
          }
          if (item != null) {
            item.delete();
          }
        }
      }
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      }
    }

    Must I redirect the page or something like that?

    I ask me if it's possible to have a progress bar during the upload?

    I use gxt 2.2.4 and gwt 2.3.0.

    Thanks a lot for the help.
    Last edited by B.Oury; 20 Apr 2012 at 4:47 AM. Reason: correction

  2. #2
    Ext GWT Premium Member
    Join Date
    Sep 2011
    Posts
    14
    Vote Rating
    0
    B.Oury is on a distinguished road

      0  

    Default


    No one has an idea?

  3. #3
    Sencha User
    Join Date
    Feb 2011
    Posts
    1
    Vote Rating
    0
    sandeep.e is on a distinguished road

      0  

    Default Progress Bar in during file upload

    Progress Bar in during file upload


    In order to add a progress bar, you would need to process the servlet's response in client side. Then only you will be able to decide on when to stop the progress bar. In client side you can do some thing similar to this


    FileUploadForm.this.addListener(Events.Submit, new Listener<FormEvent>() {


    //EXT-FIX: public void onActionComplete(Form arg0, int arg1, String arg2) {
    // }


    public void handleEvent(FormEvent arg0) {
    bar.stop();
    bar.hide();
    ClientUtils.alert("Result"+ arg0.getResultHtml());
    }
    });

    And the bar.start before you call the panel.submit();

    And in the servelet if you send a response back with the status of the request you will be able to check whether the request is processed successfully or not.
    The file size you can check in servlet and you can send a response with an error

    HTH
    Sandeep

Thread Participants: 1

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi