Skip to content

Instantly share code, notes, and snippets.

@adamsilver
Created August 22, 2016 14:45
Show Gist options
  • Save adamsilver/9f2c5437690b6cb7765f9e715bd3f2db to your computer and use it in GitHub Desktop.
Save adamsilver/9f2c5437690b6cb7765f9e715bd3f2db to your computer and use it in GitHub Desktop.
AJAX File Upload Code
<html>
<head>
<script>
window.onload = function() {
parent.helloworld(200, {"message":"","data":{"current_url":"","name":"Home","page_type":"standard","entity_uuid":null,"page_state":null,"uuid":"f7280510-3cf6-012e-7987-12313d034988","template":null,"asset_uuids":[],"user_uuid":null,"site_uuid":"a682cb70-3cf6-012e-74a4-12313d034988","navigation":false,"locked":false,"style":null}});
}
</script>
</head>
<body></body>
</html>
gd.IframeUploader = function(options) {
this.options = $.extend({
success: null,
invalid: null,
error: null,
url: null,
form: null
}, options);
do {
this.uploadId = "iframeUpload"+ new Date().getTime() + Math.floor(Math.random() * 1000);
} while (typeof(window[this.uploadId]) !== 'undefined'); //ensure no clashes if two requests at same time (to the millisecond)
window[this.uploadId] = null; //anything except undefined will reserve this id
this.iframe = null;
this.setUrl();
}
gd.IframeUploader.prototype.setUrl = function() {
this.options.url += ".iframe?callback="+this.uploadId;
};
gd.IframeUploader.prototype.createFunction = function() {
window[this.uploadId] = $.proxy(function(status, data) {
if (this.removeFormOnCallback === true && this.options.form && this.options.form.parentNode) {
this.options.form.parentNode.removeChild(this.options.form);
this.removeFormOnCallback = false;
}
if(this.options.success && this.isOk(status)) {
this.options.success(data);
}
else if( this.options.invalid && this.isInvalid(status, data) ){
this.options.invalid(data);
}
else if( this.options.error && this.isError(status, data)) {
this.options.error(data);
}
try {
delete window[this.uploadId];
} catch(e) {
window[this.uploadId] = null;
}
// defer removing iframe until after this method has returned to the \
// iframe window's control (otherwise we can get "resource not loaded" error)
setTimeout($.proxy(this.removeIframe, this), 0);
}, this);
};
gd.IframeUploader.prototype.createIframe = function() {
this.iframe = $('<iframe id="'+this.uploadId+'" name="'+this.uploadId+'" style="display: none"></iframe>');
$("body").append(this.iframe);
};
gd.IframeUploader.prototype.removeIframe = function() {
this.iframe.remove();
};
gd.IframeUploader.prototype.send = function() {
this.createFunction();
this.createIframe();
this.options.form.target = this.uploadId;
this.options.form.action = this.options.url;
// ensure accept-charset is set to UTF-8
$(this.options.form).attr("accept-charset", "utf-8");
$(this.options.form).append('<input type="hidden" name="_utf8" value="&#9731;">'); // necessary to force utf-8: http://railssnowman.info/
//append form to document if it is already not to ensure it submits in Firefox
if ($(this.options.form).closest('body').length === 0) {
this.removeFormOnCallback = true;
$('body').append(this.options.form);
}
this.options.form.submit();
};
gd.IframeUploader.prototype.isOk = function(status) {
return status >= 200 && status < 300;
};
gd.IframeUploader.prototype.isInvalid = function(status, data) {
return status >= 400 && status < 500;
};
gd.IframeUploader.prototype.isError = function(status, data) {
return status < 200 || (status >= 300 && status < 400) || status >= 500;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment