Package org.itsnat.comp.iframe

Examples of org.itsnat.comp.iframe.HTMLIFrameFileUpload


        button.addEventListener("click", this);
    }

    public void handleEvent(Event evt)
    {
        final HTMLIFrameFileUpload iframeUpload = iframe.getHTMLIFrameFileUpload(input.getHTMLInputElement());
        ItsNatServletRequestListener listener = new ItsNatServletRequestListenerSerial()
        {
            public void processRequest(ItsNatServletRequest request, ItsNatServletResponse response)
            {
                FileUploadRequest fileUpReq = iframeUpload.processFileUploadRequest(request, response);

                StringBuilder html = new StringBuilder();
                html.append("<html><head /><body>");
                html.append("<p>Content Type:\"" + fileUpReq.getContentType() + "\"</p>");
                html.append("<p>Field Name:\"" + fileUpReq.getFieldName() + "\"</p>");
                html.append("<p>File Name:\"" + fileUpReq.getFileName() + "\"</p>");
                html.append("<p>File Size:\"" + fileUpReq.getFileSize() + "\"</p>");

                FileInputStream fileOrig = null;
                try
                {
                    fileOrig = new FileInputStream(fileUpReq.getFileName());
                }
                catch(IOException ex)
                {
                    html.append("<h2>Cannot test a local file</h2>");
                }

                try
                {
                    InputStream fileUp = fileUpReq.getFileUploadInputStream();
                    long count = 0;
                    if (fileOrig != null)
                    {
                        int b1,b2;
                        do
                        {
                            b1 = fileOrig.read();
                            b2 = fileUp.read();
                            if (b1 != b2) throw new RuntimeException("Different Files");
                            count++;
                        }
                        while ((b1 != -1) && (b2 != -1));
                    }
                    else
                    {
                        int b;
                        do
                        {
                            b = fileUp.read();
                            count++;
                        }
                        while (b != -1);
                    }

                    count--; // Quitamos la lectura del -1
                    long fileSize = fileUpReq.getFileSize();
                    if (fileSize != count) throw new RuntimeException("Wrong File Size");
                    html.append("</body></html>");

                    ServletResponse servRes = response.getServletResponse();
                    servRes.getWriter().print(html.toString());
                }
                catch(IOException ex)
                {
                    throw new RuntimeException(ex);
                }
            }
        };
        iframeUpload.addItsNatServletRequestListener(listener);
    }
View Full Code Here


        EventListener timerListener = new EventListener() {
            public void handleEvent(Event evt) { }   // Nothing to do, this timer just update the client with the current state of progressElem
        };
        final ItsNatTimerHandle timerHnd = timer.schedule(null,timerListener,0,1000);

        final HTMLIFrameFileUpload iframeUpload = iframe.getHTMLIFrameFileUpload(clientDoc,input.getHTMLInputElement());

        ItsNatServletRequestListener listener = new ItsNatServletRequestListener()
        {
            public void processRequest(ItsNatServletRequest request, ItsNatServletResponse response)
            {
                FileUploadRequest fileUpReq = iframeUpload.processFileUploadRequest(request, response);

                try
                {
                    ServletResponse servRes = response.getServletResponse();
                    Writer out = servRes.getWriter();
                    out.write("<html><head /><body>");
                    out.write("<p>Content Type: \"" + fileUpReq.getContentType() + "\"</p>");
                    out.write("<p>Field Name: \"" + fileUpReq.getFieldName() + "\"</p>");
                    out.write("<p>File Name: \"" + fileUpReq.getFileName() + "\"</p>");
                    out.write("<p>File Size: " + fileUpReq.getFileSize() + "</p>");
                    out.write("</body></html>");

                    long fileSize = fileUpReq.getFileSize();
                    if (fileSize == 0) return;

                    byte[] buffer = new byte[10*1024];
                    InputStream fileUp = fileUpReq.getFileUploadInputStream();
                    long count = 0;
                    int read = 0;
                    do
                    {
                        if (iframeUpload.isDisposed())
                            return;

                        try { Thread.sleep(50); }catch(InterruptedException ex){ }
                        count += read;

                        long per = (count * 100) / fileSize;
                        synchronized(itsNatDoc)
                        {
                            Text text = (Text)progressElem.getFirstChild();
                            text.setData(String.valueOf(per));
                        }

                        read = fileUp.read(buffer);
                    }
                    while (read != -1);
                }
                catch(IOException ex)
                {
                    throw new RuntimeException(ex);
                }
                finally
                {
                    synchronized(itsNatDoc)
                    {
                        timerHnd.cancel();
                    }
                }
            }
        };
        iframeUpload.addItsNatServletRequestListener(listener);
    }
};
button.addEventListener("click", listener);

    }
View Full Code Here

TOP

Related Classes of org.itsnat.comp.iframe.HTMLIFrameFileUpload

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.