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.