Load a file with other variables in ActionScript 3 using URLLoader?

I am trying to load a file via URLLoader in ActionScript 3, I know it is possible, at least according to the docs, but I cannot figure it out. So if anyone has done this before, I would like to know what I am missing, in particular I am not sure about the URLRequest and its data property. I know my files are supposed to go there, but I'm not sure how.

Here's a very rudimentary form of the code I'm working with:

//===============================================
public function sendRequest():void {
//===============================================

  var newFile:FileReference(); //this eventually gets data loaded to it before I make request
  var sendForm:URLLoader = new URLLoader();
  var urlString:String = "/proposal_request/?";
  var header:URLRequestHeader = new URLRequestHeader("Content-Disposition: attache[attachment]; filename=" + newFile.name);
  ...

  ...
  urlString += "variable=" + instance_name.text;
  urlString += "another_variable=" + another_instance_name.text;
  ...

  ...
  var requester:URLRequest = new URLRequest(urlString);
  requester.contentType = "multipart/form-data";
  requester.method = URLRequestMethod.POST;
  requester.requestHeaders.push(header);
  requester.data = newFile; //here where I'm most confused, should this be encoded?
  ...

  ...
  sendForm.addEventListener(HTTPStatusEvent.HTTP_STATUS, responseStatus);
  sendForm.load(requester);

}

      

0


a source to share


2 answers


You can always go like this, but you really don't need to.

The FileReference class provides you with an easy way to upload a file: the upload () method .

public function upload(request:URLRequest, uploadDataFieldName:String = "Filedata", testUpload:Boolean = false):void

      



Once your FileReference object has some data, just call upload, passing in a URLRequest and possibly a data field string, with more information for the server, and a boolean that will (de) activate the test upload: if your file is larger than 10KB , the flash player will try to send a 0 byte file as a connection test before loading the real file.

After that, you can simply listen to the progress, complete and load the DataComplete events to track the load.

Adobe AS3 reference to FileReference.upload ()

+2


a source


The above is fine as long as you don't change the file data after viewing with FileReference. Suppose you resized the image or cropped the image before uploading, as you will upload it using FileReference.upload.

URLLoader can be used to send binary data to the server, but in this case the upload process is not available because the ProgressEvent is only fired after the image has been fully downloaded.



Is there a way to show the progress of image intermediate loading for resized images similar to that shown by the FileReference component.

0


a source







All Articles