1 Reply Latest reply on Apr 28, 2010 2:19 PM by Joe ... Ward

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

    brock_b

      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);
                      bmpd.draw(imgToUpload);
                      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);
                      loader.load(req);
                     
                      function uploadProgress(progress:ProgressEvent):void { trace("upload progress"); }
                      function uploadComplete(complete:Event):void { trace("upload progress"); }
                      function uploadStatus(status:HTTPStatusEvent):void {
                          trace("upload status");
                          trace(status.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,

       

      -BB