2 Replies Latest reply on Apr 27, 2010 8:36 AM by brock_b

    upload file (jpg) through URLRequest w/out FileReference.browse() in AIR


      Greetings all!  I'm working on a  project where I need to take batch processed images (processed through a  photoshop droplet) and upload them to a web service using HTTP POST.  I  thought I was on the right track with URLRequest and URLLoader but I'm  running into a wall.  I'm not quite sure how to get the image into the  post that is sent to the server.  I've seen plenty of examples of how it  could be done through FileReference.browse() but I need a way to  automate this without user intervention in selecting the files.


      Here is  where I've gotten so far on code but I'm stuck.


                   public function send_upload():void {
                      var  bmpd:BitmapData = new BitmapData(imgToUpload.width,imgToUpload.height);
                      var jpgenc:JPEGEncoder = new  JPEGEncoder(100);
                      var imgByteArray:ByteArray  = jpgenc.encode(bmpd);
                       var rhArray:Array = new Array(new URLRequestHeader("action",  "upload"));
                      var req : URLRequest = new  URLRequest("http://theUrlToTheHttpService")
                       var loader : URLLoader = new URLLoader();
                      var params  : URLVariables = new URLVariables();
                      params.sId=  sId.text;
                      params.cId= cId.text;
                       params.bId= bId.text;
                      params.sNum= sNum.text;
                       params.md5 = md5.text;
                      params.data =  imgByteArray;
                      req.data = params;
                       req.method = URLRequestMethod.POST;
                       req.requestHeaders = rhArray;
                       loader.addEventListener(ProgressEvent.PROGRESS, uploadProgress);
                       loader.addEventListener(Event.COMPLETE, uploadComplete);
                       loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, uploadStatus);
                      function  uploadProgress(progress:ProgressEvent):void { trace("upload progress"); }
                       function uploadComplete(complete:Event):void { trace("upload  progress"); }
                      function  uploadStatus(status:HTTPStatusEvent):void {
                           trace("upload status");


      I'm probably way off in left  field here.  I tried loading the image data into a bytearray and then  sending the byte array as one of the URLVariables but that doesn't seem  to be working.  Somewhere I think I need to let it know that the post  needs to be a Multipart-MIME POST but I'm not sure where to put that  either.  First things first though... is there a way to upload a file as  the same time as other variables without having to have a user select  the file first?


      Thanks so much for your assitance,



        • 1. Re: upload file (jpg) through URLRequest w/out FileReference.browse() in AIR

          In AIR you got the File class to load files without the confirmation of the user. Take a look at http://livedocs.adobe.com/flex/3/langref/flash/filesystem/File.html

          • 2. Re: upload file (jpg) through URLRequest w/out FileReference.browse() in AIR
            brock_b Level 1

            Thanks for the suggestion.


            I found an example HERE that I thought was gonna get me there... but I'm still having an issue.


            It is communicating with the server... but it returns an error saying that the imgId is "not supplied".



            public function send_upload():void {
                            var url:String = "http://theUrlToTheHttpService";
                            var boundary:String = "-----------------------------a1b2c3d4e5";
                            var req:URLRequest = new URLRequest(url);
                            req.requestHeaders = new Array(new URLRequestHeader("action", "upload"));
                            req.useCache = false;
                            req.contentType = "multipart/form-data, boundary="+boundary;
                            req.cacheResponse = false;
                            req.method = "POST";
                            var imgFile:File = new File;
                            imgFile.nativePath = dir + "0000192.jpg";
                            var fileStream:FileStream = new FileStream();
                            fileStream.open(imgFile, FileMode.READ);
                            var fileContents:ByteArray = new ByteArray();
                            fileStream.readBytes(fileContents, 0, imgFile.size);
                            var buffer:ByteArray = new ByteArray();
                            buffer.writeUTFBytes( "--"+boundary+"\r\n" );
                            buffer.writeUTFBytes( "Content-Disposition: form-data; name=\"imgId\";\r\n" );
                            buffer.writeUTFBytes( claimIdUpload.text );
                            buffer.writeUTFBytes( "\r\n"+boundary+"\r\n" );
                            buffer.writeUTFBytes( "Content-Disposition: form-data; name=\"data\"; filename=\""+imgFile.name+"\"\r\n" );
                            buffer.writeUTFBytes( "Content-Transfer-Encoding: binary\r\n" );
                            buffer.writeUTFBytes( "Content-Length: "+imgFile.size+"\r\n" );
                            buffer.writeUTFBytes( "Content-Type: application/octet-stream\r\n" );
                            buffer.writeUTFBytes( "\r\n" );
                            buffer.writeBytes(fileContents, 0, fileContents.length);
                            buffer.writeUTFBytes( "\r\n"+boundary+"--\r\n" );
                            req.data = buffer;
                            var loader:URLLoader = new URLLoader();
                            loader.addEventListener(ProgressEvent.PROGRESS, function(e:Event):void{});
                            loader.addEventListener(IOErrorEvent.IO_ERROR, function(e:Event):void { trace( 'error: ' + e.toString()); });
                            loader.addEventListener(Event.COMPLETE, function(e:Event):void { trace( loader.data ); });


            Here is what I'm sending.  Any other suggestions would be greatly appreciated.