Assuming you want to do this in pure JavaScript/JQuery - you'll need to use an iframe as the target of a form, there is a caveat - you will not be able to use the return data (the ID / success of the call) because of the same origin policy.
First create a form that will hold the file input and any other variables you wish to set:
<form id="upload-photo-form">
<input name="source" id="source" size="27" type="file" />
<input name="message" id="message" type="text" value="message example please ignore" />
</form>
Here is the main function used which creates an iframe, points the form to use it, and then retrieves the latest photo from the album using FQL.
function fileUpload(form, action_url, div_id) {
// Create an iframe
var iframe = document.createElement("iframe");
iframe.setAttribute('id', "upload_iframe");
iframe.setAttribute('name', "upload_iframe");
iframe.setAttribute('width', "0px");
iframe.setAttribute('height', "0px");
iframe.setAttribute('border', "0");
iframe.setAttribute('style', "width: 0; height: 0; border: none;");
// Add to document.
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event to detect when upload has finished
var eventHandler = function () {
if (iframeId.detachEvent)
{
iframeId.detachEvent("onload", eventHandler);
}
else
{
iframeId.removeEventListener("load", eventHandler, false);
}
setTimeout(function() {
try
{
$('#upload_iframe').remove();
} catch (e) {
}
}, 100);
FB.api({
method: 'fql.query',
query: 'SELECT src_big,pid,caption,object_id FROM photo WHERE aid= "' + albumID + '" ORDER BY created DESC LIMIT 1'
},
function(response) {
console.log(response);
}
);
}
if (iframeId.addEventListener)
iframeId.addEventListener('load', eventHandler, true);
if (iframeId.attachEvent)
iframeId.attachEvent('onload', eventHandler);
// Set properties of form...
form.setAttribute('target', 'upload_iframe');
form.setAttribute('action', action_url);
form.setAttribute('method', 'post');
form.setAttribute('enctype', 'multipart/form-data');
form.setAttribute('encoding', 'multipart/form-data');
// Submit the form...
form.submit();
}
At runtime you will presumably know the albumObjectID from a previous call, and have the access token from the session object that is returned by login or session onauthchange.
var url = 'https://graph.facebook.com/' + albumObjectID + '/photos?access_token=' + accessToken;
fileUpload($('#upload-photo-form')[0], url, $('#upload-photo-div')[0]);`
Obviously this isn't production code, there are a few things you can do to improve it's reliability (like checking the width, height, caption & tags of the submitted image to the latest image). Checking the latest image before & after the attempted upload etc.
PS: Watch out for the albumObjectID vs albumID, they are different however both can be obtained using some simple FQL queries. (eg: SELECT aid, object_id, can_upload, name FROM album WHERE owner = me() AND name = "My Album Name"
)
Hope this helps.
CameraSchoolDropout