1 Reply Latest reply on Sep 20, 2009 11:38 AM by _gilado

    URLLoader POST binary data does not work

    _gilado

      I Place my swf and a file at the same location on a web server.

       

      The swf first downloads the file then uploads some data to another file.

       

      The download works fine. The upload does not start at all.

       

      As you can see in the below code I register a listenr for every event, none are called. Nor do I get any exception.

       

      I run wireshark and can see that no traffic is generated by the upload.

       

      I'm stumped.

       


              private var startUpload:Boolean = false;
              private var uploadUrl:String;   
              private var uploadRequest:URLRequest;
              private var uploadLoader:URLLoader;
              private var uploadBytes:ByteArray;
              private var uploadStarted:Boolean = false;
              private var uploadDone:Boolean = false;

       

             public function upload(url:String):void
              {
                  uploadStarted = false;
                  uploadDone = false;
                  uploadUrl = url;
                  uploadRequest = new URLRequest(uploadUrl);
      //          uploadRequest.method = URLRequestMethod.POST;
                  uploadRequest.method = "POST";
                  uploadRequest.contentType = "binary"; // need to find the right string
                  uploadRequest.data = uploadBytes;
                  uploadLoader = new URLLoader();
                  configureUploadListeners(uploadLoader);
                  uploadLoader.dataFormat = URLLoaderDataFormat.BINARY;
                  try {
                      uploadLoader.load(uploadRequest);
                  } catch (error:Error) {
                      statusError = "upload failed: " + error.message;
                  }                                        
              }

       

              private function configureUploadListeners(dispatcher:IEventDispatcher):void
              {
                  dispatcher.addEventListener(Event.COMPLETE,uploadCompletedHandler);
                  dispatcher.addEventListener(Event.OPEN,uploadStartedHandler);
                  dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR,uploadSecurityErrorHandler) ;
                  dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS,uploadHttpStatusHandler);
                  dispatcher.addEventListener(IOErrorEvent.IO_ERROR,uploadIOErrorHandler);
              }
                 
              private function uploadStartedHandler(event:Event):void
              {
                  uploadStarted = true;
                  statusError += "Upload Started " + event;
              }

       

              private function uploadCompletedHandler(event:Event):void
              {
                  uploadDone = true;
                  statusError += "Upload Completed ";
              }

       

              private function uploadSecurityErrorHandler(event:Event):void
              {
                  statusError += "Upload SecurityError "; //(" + event + ")";
              }
              private function uploadHttpStatusHandler(event:Event):void
              {
                  statusError += "Upload HttpStatus "; //(" + event + ")";
              }
              private function uploadIOErrorHandler(event:Event):void
              {
                  statusError += "Upload IOError (" + event + ")";
              }

        • 1. Re: URLLoader POST binary data does not work
          _gilado Level 1

          It now works! These are my changes to the above code

           

                      uploadRequest.method = URLRequestMethod.POST;
                      uploadRequest.contentType = "application/octet-stream";
                      uploadBytes = downloadBytes;
                      uploadRequest.data = uploadBytes;

           

          downloadBytes is a ByteArray with the content of the downloaded binary file.

           

          I call the method with a url like this upload("http://www.mydomain.com/upload.cgi?upload_file_name");

           

          My upload.cgi looks like this

           

          #!/bin/bash

          echo "REQUEST_METHOD: $REQUEST_METHOD" > upload_log.txt
          echo "QUERY_STRING: $QUERY_STRING" >> upload_log.txt
          echo "CONTENT_LENGTH: $CONTENT_LENGTH" >> upload_log.txt
          echo "CONTENT_TYPE: $CONTENT_TYPE" >> upload_log.txt
          cat > $QUERY_STRING
          echo "Content-type: text/html"
          echo ""

           

           

          After uploadCompletedHandler executes, I find an exact py of the bytes in downloadbytes in a file called upload_file_name