Thursday, November 28, 2013

Re: GWT FileUpload with Progress Listener

I do this in the FormPanel onSubmit:

    private static final int PROGRESS_DELAY_MSEC = 1000;

    private static final int PROGRESS_UPDATE_MSEC = 1000;

    private Timer m_progressUpdateTimer;


        m_progressUpdateTimer = new Timer()

        {

            @Override

            public void run()

            {

                requestProgressUpdate();

            }

        };

        m_progressUpdateTimer.schedule(PROGRESS_DELAY_MSEC);

...

    private void requestProgressUpdate()

    {

        if (DEBUG)

        {

            Log.log(Duration.currentTimeMillis()/1000 + " requestProgressUpdate");

        }

        RequestBuilder builder = new RequestBuilder(GET, FILE_UPLOAD_SERVLET);

        try

        {

            builder.sendRequest(null, new RequestCallback()

            {

                @Override

                public void onResponseReceived(Request p_request,

                                               Response p_response)

                {

                    m_progress.setVisible(true);

                    if (p_response.getStatusCode() == Response.SC_OK)

                    {

                        String text = p_response.getText();

                        if (DEBUG)

                            m_status.setHTML(text);

                        if (m_progressUpdateTimer != null && text.length() > 0)

                            updateProgress(text);

                        if (DEBUG)

                        {

                            Log.log(Duration.currentTimeMillis()/1000

                               + " onResponseReceived " + text + " (OK)");

                        }

                    }

                    else

                    {

                        String text = p_response.getStatusText();

                        m_status.setHTML(text);

                        if (DEBUG)

                        {

                            Log.log(Duration.currentTimeMillis()/1000

                               + " onResponseReceived " + text + " ("+p_response.getStatusCode()+")");

                        }

                    }

                    if (m_progressUpdateTimer != null && isAttached())

                        m_progressUpdateTimer.schedule(PROGRESS_UPDATE_MSEC);

                }

                @Override

                public void onError(Request p_request, Throwable p_e)

                {

                    m_progress.setVisible(true);

                    m_status.setText(p_e.getMessage());

                    if (m_progressUpdateTimer != null && isAttached())

                        m_progressUpdateTimer.schedule(PROGRESS_UPDATE_MSEC);

                    if (DEBUG)

                    {

                        Log.log(Duration.currentTimeMillis()/1000

                           + " onError " + p_e.getMessage());

                    }

                }

            });

        }

        catch (RequestException e)

        {

            m_progress.setVisible(true);

            m_status.setText(e.getMessage());

            if (DEBUG)

            {

                Log.log(Duration.currentTimeMillis()/1000

                   + " RequestException " + e.getMessage());

            }

        }

    }


    private void updateProgress(String p_text)

    {

        try

        {

            Document xml = XMLParser.parse(p_text);

            Integer item = intValue(xml, "item");

            if (item == null)

            {

                m_progress.setItem(null);

            }

            else

            {

                // form item 1 is the id, items 2+ are the files.

                String filename = null;

                if (item > 1 && item - 2 < m_uploadFilelist.size())

                    filename = m_uploadFilelist.get(item - 2);

                m_progress.setItem(filename);

            }

            Integer percent_complete = intValue(xml, "percent_complete");

            if (percent_complete != null)

            {

                m_progress.setProgress(percent_complete.doubleValue());

            }

            if (DEBUG)

            {

                Integer bytes_read = intValue(xml, "bytes_read");

                Integer content_length = intValue(xml, "content_length");

                String progress = "Uploaded " + bytes_read + " of "

                        + content_length + ": " + percent_complete + "%";

                m_status.setText(progress);

            }

        }

        catch (Exception e)

        {

            m_status.setText(e.getMessage());

            if (DEBUG)

            {

                Log.log(Duration.currentTimeMillis()/1000

                   + " updateProgress " + e.getMessage());

            }

        }

    }


    private Integer intValue(Document p_xml, String p_tagname)

    {

        try

        {

            NodeList list = p_xml.getElementsByTagName(p_tagname);

            if (list.getLength() > 0)

            {

                Node node = list.item(0);

                list = node.getChildNodes();

                node = list.item(0);

                String value = node.getNodeValue();

                if (value != null)

                    return Integer.valueOf(value);

            }

        }

        catch (Exception e)

        {

            m_status.setText(e.getMessage());

        }

        return null;

    }


    private static class UploadProgressBar extends ProgressBar

    {

        private String m_item;


        public UploadProgressBar()

        {

            super();

        }


        public void setItem(String p_item)

        {

            m_item = p_item;

        }


        @Override

        protected String generateText(double curProgress)

        {

            if (m_item == null)

                return (int)(100 * getPercent()) + "%";

            else

                return m_item + " " + (int)(100 * getPercent()) + "%";

        }

    }



doGet in my FileUploadServlet does this:


    @Override

    protected void doGet(HttpServletRequest p_request,

                         HttpServletResponse p_response)

        throws ServletException, IOException

    {

        if (DEBUG)

            System.err.println("\n" + System.nanoTime() + " doGet request "

                + p_request.getContentType() + " " + p_request.getServletPath());


        HttpSession session = p_request.getSession();

        if (session == null)

            return;


        FileUploadProgress progress =

            (FileUploadProgress)session.getAttribute("progress");

        if (progress == null)

            return;


        long bytesRead = progress.getBytesRead();

        long contentLength = progress.getContentLength();

        int item = progress.getItem();


        p_response.setContentType("text/xml");


        StringBuffer buffer = new StringBuffer();

        buffer.append("<?xml version=\"1.0\"?>\n");

        buffer.append("<response>\n");

        buffer.append("\t<item>").append(item)

              .append("</item>\n");

        buffer.append("\t<bytes_read>").append(bytesRead)

              .append("</bytes_read>\n");

        buffer.append("\t<content_length>").append(contentLength)

              .append("</content_length>\n");


        long percentComplete;

        if (bytesRead < contentLength && contentLength != 0)

        {

            percentComplete = ((100 * bytesRead) / contentLength);

        }

        else

        {

            percentComplete = 100;

            session.setAttribute("progress", null);

        }


        buffer.append("\t<percent_complete>").append(percentComplete)

              .append("</percent_complete>\n");

        buffer.append("</response>\n");


        PrintWriter out = p_response.getWriter();

        out.println(buffer.toString());

        out.flush();

        out.close();

    }


FileUploadProgress uses this Apache Commons package:

http://commons.apache.org/proper/commons-fileupload/

import org.apache.commons.fileupload.ProgressListener;


public class FileUploadProgress implements ProgressListener

{

    private volatile long m_bytesRead;

    private volatile long m_contentLength;

    private volatile int m_item;


    public FileUploadProgress()

    {

        super();

    }


    @Override

    public void update(long p_bytesRead, long p_contentLength, int p_item)

    {

        m_bytesRead = p_bytesRead;

        m_contentLength = p_contentLength;

        m_item = p_item;

    }


    public long getBytesRead()

    {

        return m_bytesRead;

    }


    public long getContentLength()

    {

        return m_contentLength;

    }


    public int getItem()

    {

        return m_item;

    }

}



On Thursday, November 28, 2013 9:42:31 AM UTC-8, confile wrote:
Yes, but I do not get how to use it. Can you help me with this please?

Am Donnerstag, 28. November 2013 12:20:21 UTC+1 schrieb Thomas Broyer:
Have you checked Elemental? AFAICT, everything's there already.
http://www.gwtproject.org/articles/elemental.html

On Wednesday, November 27, 2013 4:02:48 PM UTC+1, confile wrote:

 want to observe the upload percentage of a file upload from GWT.

In JavaScript you can use a XMLHttpRequest and add an event listener like this:

var oReq = new XMLHttpRequest();    oReq.upload.addEventListener("progress", updateProgress, false);    // progress on transfers from the server to the client (downloads)  function updateProgress (oEvent) {    if (oEvent.lengthComputable) {      var percentComplete = oEvent.loaded / oEvent.total;      // ...    } else {      // Unable to compute progress information since the total size is unknown    }  }

(The above code is from here.)

This is also done very easily in jQuery as:

 var $request = $.ajax({        xhr: function() {          xhrNativeObject = new window.XMLHttpRequest();          //Upload progress          xhrNativeObject.upload.addEventListener("progress", function(event) { ... }        }   });

I want to do the same with GWT. I could use a RequestBuilder to send a request, but this is only a high level wrapper around the XMLHttpRequest JavaScriot object. Another possibility would be to use the GWT XMLHttpRequest class which is a JSNI wrapper of the JavaScript XMLHttpRequest.

My problem:

How can I add a progress listener to the XMLHttpRequest or the RequestBuilder?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment