« May 2008 | Main | July 2008 »

June 2008 Archives

June 11, 2008

How to receive data in response to a file upload in Flex

If you are not developing an Adobe AIR application, the only way to work with your filesystem is to upload a file to your server, so probably you have already faced the problem of receiving data in response to a file upload.
To upload a file you can use the upload function of a FileReference object; obviously this can be done only after having browsed for a file and opened it; this function takes three arguments, a URLRequest that represents the server page, a String that will be passed as the first “form-data” of the POST request, and a Boolean. Only the first one is mandatory.
According to the Flex 3 API there are different events dispatched by a FileReference object during upload execution:

  • open:Event
  • progress:ProgressEvent
  • complete:Event
  • uploadCompleteData:Event
  • securityError:SecurityErrorEvent
  • httpStatus:HTTPStatusEvent
  • httpResponseStatus:HTTPStatusEvent
  • ioError:IOErrorEvent

When you receive a complete Event your file has been uploaded successfully, but you have no server response.
To read the response from the server you should use the uploadCompleteData event.
This event has been introduced in Flash Player 9.0.28, so having this version is a minimum requirement; you will be able to add the event listener but with a lower flash player version this event will never be dispatched.

This is a simple servlet that receive an uploaded file; written using the file upload utils from apache.org: http://commons.apache.org/fileupload/


protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

doProcess(req, resp);

}



protected  void doPost(HttpServletRequest req, HttpServletResponse resp) {

doProcess(req, resp);

}



protected void doProcess(HttpServletRequest req, HttpServletResponse resp) {



     String fileName = "";

     InputStream is = null;

          

            

            DiskFileItemFactory factory = new DiskFileItemFactory(4096000, new File(System.getProperty("java.io.tmpdir")));

            ServletFileUpload sfu = new ServletFileUpload(factory);

            List<FileItem> fileItems = sfu.parseRequest(request);

            Iterator<FileItem> it = fileItems.iterator();

            

            

            while(it.hasNext()){

             FileItem fileItem = it.next();

             if(!fileItem.isFormField()) {

             fileName = fileItem.getName();

             is = fileItem.getInputStream();

                        //do something with the file input stream



             }

            }

            

            resp.getWriter().write("Upload successfull");



}

This is the actionscript code to read the response. First of all add an the event listener to the file reference instance with which you browsed and opened the file:


fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);

This simple responseHandler shows an Alert with the string received from the server:


private function responseHandler( event:DataEvent ) :void

{

          Alert.show(event.data as String);

}

Obviously this example is quite useless because an upload confirmation arrives from the "complete" event; but in a lot of cases we could receive useful information such as a detailed error, or the id of a new record in a database relative to the file upload or the file itself, transforming the servlet in a mean to read text files from the local filesystem, of course this file cannot be too large due to the bandwith and upload size limit.

June 20, 2008

Creating and accessing JavaScript objects from ActionScript classes in AIR

These days I'm working on the O'Reilly AIR Cookbook creating the examples for the chapter on the HTMLLoader class.
The HTMLLoader class has a powerful method that lets you to load html content from a simple html string. The method is part of the public methods of the HTMLLoader class: loadString().
It accepts a parameter that contains the html content to load within an istance of the HTMLLoader class.

With this simple code you'll load the htmlToLoad string into the HTMLLoader class. The _html istance will render as HTML the content through the WebKit engine.

NOTE: Due to formatting problem I've posted the code examples and the ActionScript 3 class for this post on my personal blog :
Creating JavaScript functions within an ActionScript class in AIR

The cool thing is that you can use this approach to write JavaScript objects, functions and properties as well as defining the HTML structure within an ActionScript class, and then access to the html DOM via ActionScript code.

In the next page I've created an example where an HTML content is created within an ActionScript class and then loaded into an HTMLLoader object. I've accessed using ActionScript to the HTML DOM to get the content within the DIV tag.

Consider these three important considerations:
a) access to the DOM elements and JavaScript objects only after the page load event is dispatched. The page load event corresponds to the COMPLETE event dispatched by the HTMLLoader class. You can create an event listener for this event using the addEventListener() method:

_html.addEventListener(Event.COMPLETE, onComplete);

b) you can access to DOM elements using the window.document object and invoking the getElementById, and getElementsByTagNamer() methods. The window object represents the global JavaScript object for the content loaded into the HTML control.
c) you can edit and create the content of the html content using the innerText and innerHTML properties

NOTE: Due to formatting problem I've posted the code examples and the ActionScript 3 class for this post on my personal blog :

Creating JavaScript functions within an ActionScript class in AIR

About June 2008

This page contains all entries posted to Comtaste Consulting | Enterprise RIA consulting and development in June 2008. They are listed from oldest to newest.

May 2008 is the previous archive.

July 2008 is the next archive.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33