onSubmitCompleted issue?

70 views
Skip to first unread message

Thomas Lefort

unread,
Nov 12, 2012, 7:11:47 AM11/12/12
to google-we...@googlegroups.com
I implemented a simple image uploader which relies on the form panel. I pass an image file via a filupload widget and a multipart encoding to the server which then processes the image, stores it on the disk and returns the image file path as a text/html response. On the client side I have an onSubmitCompleted event handler that sets the image widget url to the one returned by the servlet.

The onSubmitCompleted handler is sometimes (actually quite often) not called. I checked with firebug, the servlet does return the right file path, the image is stored, etc... The servlet also gets fully executed with no error (checked with various traces). I have the behavior for (at least) FF16. I have the issue in hosted mode or in compiled ran locally (with the embedded jetty).

Here's my servlet code:

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter();
        try {
            // default values for width and height
            int width = 50;
            int height = 50;
            InputStream filecontent = null;
            String saveFile = null;
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    String fieldName = item.getFieldName();
                    String fieldValue = item.getString();
                    if(fieldName.equalsIgnoreCase("width")) {
                        width = Integer.parseInt(fieldValue);
                    }
                    if(fieldName.equalsIgnoreCase("height")) {
                        height = Integer.parseInt(fieldValue);
                    }
                } else {
                    String fieldName = item.getFieldName();
                    saveFile = FilenameUtils.getName(item.getName());
                    if(fieldName.equalsIgnoreCase("image")) {
                        filecontent = item.getInputStream();
                    }
                }
            }
            // check values
            if(filecontent == null) {
                throw new FileNotFoundException("no file provided");
            }
            // Process the input stream
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int len;
            byte[] buffer = new byte[8192];
            while ((len = filecontent.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, len);
            }

            String imagePath = processAndStoreImage(out, saveFile, width, height);
            System.out.println(imagePath);
           
            // return the url of the file
            writer.println(imagePath);

         } catch (Exception e) {
            writeError(writer, "Error whilst processing and storing image.");
        } finally {
            writer.flush();
            writer.close();
        }

    }

and the onSubmitHandler

        imageForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
            public void onSubmitComplete(SubmitCompleteEvent event) {
                final String result = event.getResults();
                // update thumbnail view with new image
                thumbnailImage.setUrl(result);
Window.alert("YES");
            }
        });




Thomas Broyer

unread,
Nov 12, 2012, 7:47:05 AM11/12/12
to google-we...@googlegroups.com
Because Content-Types are so often wrong on the internet (particularly when set to text/html or text/plain), browsers use content-sniffing. Because your response does not look like HTML, it might very-well be sniffed as text/plain by the browser, which will then skip the onSubmitComplete.
Try prepending "<html><body>" to your response so that browsers will "sniff" it as text/html (or in other words, send back HTML, not something that's only *labelled* as being HTML)

Thomas Lefort

unread,
Nov 12, 2012, 8:23:02 AM11/12/12
to google-we...@googlegroups.com
Hi Thomas,

Thanks for the suggestion. I would have never thought of something like that! I tried your suggestion but unfortunately without success. That said, discussing always generates ideas and I had another look at the code. It turned out to be much more trivial than that: I was hiding the form panel before the submit completed handler is called... I guess I should have posted the whole code (a bit long though) ;-) I changed the implementation and it looks like it is working now. (it doesn't explain how it still managed to work sometimes though).

I kept your suggestion to avoid content sniffing issues in the future. Thanks again for a great help!
Reply all
Reply to author
Forward
0 new messages