What it appears to be, it is pretty easy to send single file to the server, however to send multiple files is a little more trickier.
I have created an example below how it can be achieved with the HTML and Javascript Adobe Air SDK.

var requestBoundary = "----------GI3ei4cH2Ij5GI3cH2Ij5cH2ei4Ef1";

var request = new air.URLRequest("http://url_to_send_to");
request.useCache = false;
request.contentType = 'multipart/form-data, boundary=' + requestBoundary;
request.method = air.URLRequestMethod.POST;

var buffer = new air.ByteArray();

//create below parameters that you want to send to the server
buffer = this.appendBuffer(buffer, "full_name", "Dima Svirid");
buffer = this.appendBuffer(buffer, "email", "test@gmail.com");

//below lets specify files to be sent
buffer = this.appendBuffer(buffer, "attachment_1", air.File.applicationStorageDirectory.resolvePath("path_to_file_1"));
buffer = this.appendBuffer(buffer, "attachment_2", air.File.applicationStorageDirectory.resolvePath("path_to_file_1"));

//wrap it all up at the end
buffer.writeUTFBytes( "--" + requestBoundary + "--\r\n" );

request.data = buffer;

var loader = new air.URLLoader();
loader.addEventListener(air.Event.COMPLETE, function() {alert('Success!')});
loader.addEventListener(air.IOErrorEvent.IO_ERROR, function() {alert('Error!')}); //this line is required

//send it
loader.load( request );

Let’s create a helpful method to generate raw content to be sent

var appendBuffer = function(buffer, param_name, param_value) {
  buffer.writeUTFBytes( "--" + requestBoundary + "\r\n" );

  //lets handle it as a file
  if(typeof param_value == 'object') {
	  var fileStream = new air.FileStream();
	  fileStream.open(param_value, air.FileMode.READ);
	  var content = new air.ByteArray();
	  fileStream.readBytes(content, 0, param_value.size);

	  buffer.writeUTFBytes( "Content-Disposition: form-data; name=\"" + param_name + "\"; filename=\"" + param_value.name + "\"\r\n" );
	  buffer.writeUTFBytes( "Content-Transfer-Encoding: binary\r\n" );
	  buffer.writeUTFBytes( "Content-Length: " + param_value.size + "\r\n" );
	  buffer.writeUTFBytes( "Content-Type: application/octet-stream\r\n" );
	  buffer.writeUTFBytes( "\r\n" );
	  buffer.writeBytes(content, 0, content.length);
	  buffer.writeUTFBytes( "\r\n" );

  } else {
	  buffer.writeUTFBytes( "Content-Disposition: form-data; name=\"" + param_name + "\"\r\n" );
	  buffer.writeUTFBytes( "\r\n" + param_value + "\r\n" );

  return buffer;

Dima Svirid

Software architect, JAVA, Spring, Hibernate, AngularJs, Backbone, MongoDB, Oracle. CTO and Co-Founder of Homeadnet.com

More Posts

Follow Me: