Blog
Adobe AIR, save bandwidth uploading files!
8 years, 3 months ago Posted in: Blog Comments Off

Today I've discovered an improvement to file uploads from AIR.

Using the upload method from flash.filesystem.File , we'lll send the bytearray uncompressed to a server service. great. However with this little script we can save large amount of bandwidth compressing the bytearray data before sending.

As always feel free to use it and abuse it.

hasta luego!

Actionscript:
  1. package com.mattimatti.remote.io
  2. {
  3.     import flash.net.URLLoader;
  4.     import flash.net.URLRequest;
  5.     import flash.net.URLRequestHeader;
  6.     import flash.net.URLRequestMethod;
  7.     import flash.utils.ByteArray;
  8.  
  9.  
  10.     /**
  11.      * FileUploader class
  12.      *
  13.      * @author Matteo Monti  http://www.mattimatti.com
  14.      *
  15.      * Utility class to send a bytearray to a server side script.
  16.      *
  17.      * It differs from method flash.filesystem.File.upload() giving the option to compress bytearray data
  18.      * before sending. this results in great advantage in terms bandwidth usage.
  19.      *
  20.      *
  21.      * var  fu:FileUploader = new FileUploader();
  22.      *   fu.addEventListener(Event.COMPLETE,completeHandler);
  23.      *   fu.upload("http://www.somehost/service.php", ba,"somefilename.ext");
  24.      *
  25.      *
  26.      * Original script from:
  27.      * from: http://blog.joa-ebert.com/2006/05/01/save-bytearray-to-file-with-php/
  28.      *
  29.      *
  30.      * PHP SCRIPT
  31.      *
  32.      *
  33.      * $headers = apache_request_headers();
  34.      * $fp = fopen( $headers["dest-file-name"], "wb" );
  35.      * fwrite( $fp, gzuncompress($GLOBALS[ 'HTTP_RAW_POST_DATA' ]) );
  36.      * fclose( $fp );
  37.      *
  38.      *
  39.      *
  40.      */
  41.     public class FileUploader extends URLLoader
  42.     {
  43.         public function FileUploader()
  44.         {
  45.             super();
  46.         }
  47.        
  48.        
  49.  
  50.        
  51.        
  52.         /**
  53.          * saves to server side script a bytearray 
  54.          *
  55.          * @param inUrl             the remote service
  56.          * @param inByteArray     the file ontent
  57.          * @param inFileName       the destination filename
  58.          * @param inCompress       do we use compressionover bytearray? yes to make the difference!
  59.          *
  60.          */
  61.         public function upload(inUrl:String, inByteArray:ByteArray, inFileName:String, inCompress:Boolean = true):void
  62.         {
  63.            
  64.             if(!inFileName)throw new Error("NO FILE NAME SPECIFIED");
  65.            
  66.            
  67.             var request:URLRequest = new URLRequest (inUrl);
  68.                 request.contentType = "application/octet-stream";
  69.                 request.method = URLRequestMethod.POST;
  70.                
  71.                 // will compress bytearray to be uncompressed later
  72.                 if(inCompress){
  73.                     inByteArray.compress();
  74.                     inByteArray.position = 0;
  75.                 }
  76.                
  77.                 request.data = inByteArray;
  78.                
  79.                 var header:URLRequestHeader=new URLRequestHeader("dest-file-name",inFileName);
  80.                
  81.                 request.requestHeaders.push(header);
  82.                
  83.                 load( request );
  84.         }
  85.                
  86.        
  87.        
  88.     }
  89. }

Comments are closed.

Image compression on this page is provided by WordPress Image Compressor