Uploading video from iPad to server with AIR for iOS

In this part 2 post following on from Record and play back video with AIR for iOS on iPad I will show you how to take your freshly recorded video and upload it to a web server using PHP.

Assuming you have a MovieClip or some other button labeled ‘uploadbtn’ you first need to add a click event handler to it to trigger the upload.

[cc lang=”actionscript3″]
uploadbtn.addEventListener(MouseEvent.CLICK, onUpload);
[/cc]

Now for the click handler:

[cc lang=”actionscript3″]
function onUpload(e:MouseEvent):void
{
// create a URLRequest for the PHP file on you server (we’ll get to the PHP later)
var URLrequest:URLRequest = new URLRequest(“http://www.yourdomain.com/uploadFile.php”);

// videoFile is a File object we created in the last post which references the recorded video
// attach the various listeners for errors, progress, complete
videoFile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadDataComplete);
videoFile.addEventListener(ProgressEvent.PROGRESS, progressHandler);
videoFile.addEventListener(IOErrorEvent.IO_ERROR, handleError);
videoFile.addEventListener(Event.COMPLETE, completeHandler);

// call upload on the File object and pass in the URLRequest
videoFile.upload(URLrequest);
}
[/cc]

Now we can create the handlers for the attached listeners:

[cc lang=”actionscript3″]
function progressHandler(e:ProgressEvent):void
{
// just tracing the percentage of progress – you could show it in a progress bar
trace(“Uploading… ” + Math.ceil(100 * (e.bytesLoaded / e.bytesTotal)) + “%”);
}

function completeHandler(e:Event):void
{
// upload process is complete
trace(videoFile.name + ” has been uploaded.\n”);
}

function uploadDataComplete(e:DataEvent):void
{
//everything is complete, trace the message returned from the PHP script which is in XML format
var xml:XML = new XML(e.data);

trace(xml.message);
}

function handleError(e:IOErrorEvent):void
{
// simple error handler traces the error
trace(“Upload Error:” + e.text);
}
[/cc]

That is all that is required for the ActionScript side of things. Now for the PHP script. This is a very basic script and you should provide some security and validation on any production code.

All you need to do is save this into a blank PHP file and upload it to your server to the location matching the address in you URLRequest above.

[cc lang=”php”]
OK$file_name uploaded successfully.“;
}else{
// create a failed message if something went wrong
$message = “FAIL$file_name did not upload successfully.“;
}

echo $message;
?>
[/cc]

That is all that’s required to upload a video from the iPad to a web server. It is surprisingly simple for how hard it was for me to work out.

I hope this helps somebody out!


Comments

4 responses to “Uploading video from iPad to server with AIR for iOS”

  1. […] In this part 2 post following on from Record and play back video with AIR for iOS on iPad I will show you how to take your freshly recorded video and upload it to a web server using PHP.Assuming you have a MovieClip or some other button labeled ‘uploadbtn’ you first need to add a click event handler to it to trigger the upload.  […]

  2. Mauricio Valdes Avatar
    Mauricio Valdes

    thx , great work !!

  3. 劉珂 Avatar
    劉珂

    Dear Ben Foster, thank you very much for supplying a great solution about video process with Air for iOS. But it seems that there is a little problem when I tried to execute you code. That is the line “videoFile.upload(URLrequest);”. When the line is executed, it produces nor errors nor exceptions, but no handlers are executed either. (including the uploadDataComplete and completeHandler). Actually, I found that no http request even hit the server side. It seems that the Air SDK for iOS just failed to send the http request for some reason. I am now using Air SDK 16 and iOS8. I have been struggling for this for days. So I really appreciate it if you or someone else has any hint about this. Thanks in advance.

Leave a Reply