Skip to content

Instantly share code, notes, and snippets.

/upload.js Secret

Created March 23, 2013 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2a2fef447399a3b8e734 to your computer and use it in GitHub Desktop.
Save anonymous/2a2fef447399a3b8e734 to your computer and use it in GitHub Desktop.
fd = new FormData;
fd.append("zip", fdata);
fd.append("name", $("#ag-name").val());
$("#uploadc-s").html("<progress id='uploadprogress' max='100' value='0'></progress> <span id='progress'>Uploading...</span>");
var xhr = new window.XMLHttpRequest();
var uploaded = false;
xhr.upload.addEventListener("progress", function (e) {
if (!uploaded && e && e.lengthComputable) {
p = (e.loaded / e.total);
var percent = Math.round(p * 1000) / 10;
$("#uploadprogress").val(e.loaded);
$("#uploadprogress").attr("max", e.total);
$("#progress").text("Uploading... " + percent + "%");
}
}, false);
xhr.upload.addEventListener("load", function (e) {
uploaded = true;
$("#uploadprogress").val(false);
$("#uploadprogress").attr("max", false);
$("#progress").text("{{ "Checking file"|l }}");
}, false);
xhr.addEventListener("readystatechange", function(e) {
if (this.readyState === 4) {
data = $.parseJSON(xhr.responseText);
if (data.success) {
window.location = data.url;
} else {
msg(data.error, MSG_ERROR);
$("#uploadc-s").hide(100);
$("#uploadc")
}
}
}, false);
xhr.open("post", "ajax/upload-map", true);
xhr.send(fd);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment