How to upload a file on a server?

Hi

I am trying to upload a file to a server from my mobile game but I dont know how to handle this. Actually, I have two issues. I don’t know how to encode my file informations into the URLRequest.data field and second, I don’t know how to retrieve the server answer after the URLLoader.load() request (the php script on the server output a sucess or error message but I don’t know how to get it from my openfl mobile app).

Here is the code I have with the two problematic points:

var urlRequest:URLRequest = new URLRequest("http://localhost/test/upload.php");
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = "How to encode my file content and name here?";
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );
        
var urlLoader:URLLoader = new URLLoader();
//urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        
try {
     urlLoader.load( urlRequest );
     //How to retrieve the server response here (a message displayed by the upload.php script with the echo function)?
}
 catch (error:Error) {
            trace( " Unable to load URL: " + error);
 }

If somebody could help, it would be nice :wink:

Try formatting it as JSON, and listening for events:

var fileContents:String = "Your file contents here";
var name:String = "Your name here";

var urlRequest:URLRequest = new URLRequest("http://localhost/test/upload.php");
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = Json.stringify({name:name, content:fileContents});
urlRequest.contentType = "application/json";
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;

urlLoader.addEventListener( Event.COMPLETE, onComplete);
urlLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, onHttpStatus);
urlLoader.addEventListener( IOErrorEvent.IO_ERROR, onIOError );
urlLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );

urlLoader.load( urlRequest );

Event.COMPLETE is dispatched when and if the loader finishes uploading successfully. This will always be dispatched along with an HTTP_STATUS event.

HTTPStatusEvent.HTTP_STATUS is dispatched when the loader receives any HTTP status.

IOErrorEvent.IO_ERROR is dispatched if there’s a connection issue that causes the loader to fail.

I thinkSecurityErrorEvent.SECURITY_ERROR only happens in Flash, because OpenFL doesn’t copy Flash’s security restrictions. If you aren’t targeting Flash, (and perhaps even if you are), you can skip this one.

Well it won’t work as my php script expect a file and not a json object… (and I don’t want to change it. Because if my file is a picture for example, json format wouldn’t be the best choice to transmit the information)

And same thing for the HTTP status. I don’t want to rely on it but to be able to parse the text result returned by my upload.php script (which allows to return more complex informations than a single HTTP error code)

EDIT: I finaly achieved my goal using my own multipart post data formatter (based on this: https://gist.github.com/zhouji/4303867) and the akifox-asynchttp library to handle response. And it works perfectly!