1 Reply Latest reply on Jun 9, 2010 2:43 AM by nijlz

    File Drag, Drop and Upload

    nijlz

      Hello everybody,

       

      I got to manage Drag&Drop of files from Desktop to my Air application. If I drag images I can show them without problems. Now I want to upload them to a webspace. I found some code which seems to work, but all examples don't consider authentication, but I think I solved this problem too. The response I get back from the webserver: "Not an image or corrupt file."

      Here is my code, I hope somebody has an idea to fix it.

       

      public function onDrop(event:NativeDragEvent):void
      {
          NativeDragManager.dropAction = NativeDragActions.COPY;
          var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

          for each (var file:File in dropfiles)
          {
              if(file.extension == "jpg")
              {
                  var r:URLRequest = new URLRequest("http://localhost/...");
                  var encoder:Base64Encoder = new Base64Encoder();
                  encoder.encode("user:pass");
                  var header:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());
                  r.method = "POST";
                  r.authenticate = true;
                  r.requestHeaders.push(header);
                  r.contentType = "multipart/form-data";
                  file.upload(r);
                  file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, trace);
              }
          }
      }