« Google Maps API for Flex and Flash: some tips and notes | Main | Creating and accessing JavaScript objects from ActionScript classes in AIR »

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.

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/56

Comments (2)

Dewald:

Thank you very much for the info, my upload was failing and I could not determine what was going on! Now I can read the error messages coming from the PHP script. Keep up the good work!

mazman:

Hi,
Thanks for the illustration. I have used it to upload an XML file of size 5kb, but the event.data returns only about 3kb? all my XML files that have more than 3kb size are truncated? what is wrong with that?
thanks for help

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on June 11, 2008 4:03 PM.

The previous post in this blog was Google Maps API for Flex and Flash: some tips and notes.

The next post in this blog is Creating and accessing JavaScript objects from ActionScript classes in AIR.

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

Powered by
Movable Type 3.33