Code I/O

A topnotch site

5 Minutes on Adobe Flex: Mimic file upload for in-memory contents …

1 Comment

Ever imagined uploading a file without using FileReference.upload?

In a web-app, access to filesystem is restricted, and at times, there is no need to refer to a file.  However, there may be situation where there is need to persist in-memory contents on servers.

The idea is to send contents from internal buffer which are non-file reference objects.  The fundamental behind  this is to fill in the content headers and mimic fileupload using HTTPService and here is the snippet for spoofing it.

Remember, though one can spoof the file upload, the trigger must be coming from an UI object (like a Button click event), otherwise, Flash will fail to upload the content.

public function spoofFileUpload(inputText:String,
 faultHander:Function=null):void {

 var boundary:String = UIDUtil.createUID();
 var service:HTTPService = new HTTPService();

 service.method = "POST";
 service.contentType = "multipart/form-data; boundary=—————————" + boundary;

 var content:String = "—————————" + boundary + "nr";
 content += 'Content-Disposition: form-data; name="uploadfile"; filename="' + fileName + '";' + "nr";
 content += 'Content-Type: text/plain' + "nrnr";
 content += inputText + "nr";
 content += "—————————" + boundary + "nr";
 content += 'Content-Disposition: form-data; name="Submit"' + "nr" + "Submit" + "nr";
 content += "—————————–" + boundary + "–nr";

 service.request = content;
 service.resultFormat = HTTPService.RESULT_FORMAT_E4X;

 if(faultHander != null){
 service.addEventListener(FaultEvent.FAULT, faultHander);

 if(resultHandler != null){
 function httpResult(event:ResultEvent):void
 var xml:XML = XML(event.result);
 trace("SERVER RESPONSE: " + xml);
 // Invoke your result handler here

 // Send Request

One thought on “5 Minutes on Adobe Flex: Mimic file upload for in-memory contents …

  1. Hi there can you please help me understand your multipart/form-data example?
    1) What is “nr” there?
    2) is undeerscore before boundry really required?

    I am trying something similar but have problem in making it work,
    I have posted my problem, here on stackoverflow

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s