Last active
April 13, 2016 07:54
-
-
Save clzola/05392e6b034a2b49844c9e819d713330 to your computer and use it in GitHub Desktop.
JavaScript Ajax Upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function completeHandler(jqXHR, testStatus) { | |
console.log("Complete Handler"); | |
} | |
function errorHandler(jqXHR, textStatus, errorThrown) { | |
console.log(jqXHR, textStatus, errorThrown) | |
console.log("Error Handler"); | |
} | |
function successHandler(data, textStatus, jqXHR) { | |
console.log("Success Handler"); | |
} | |
function getXHR() { | |
var xhrctrl = new window.XMLHttpRequest(); | |
xhrctrl.upload.addEventListener("progress", function(event) { | |
if( event.lengthComputable ) { | |
var percent = event.loaded / event.total; | |
var width = Math.floor($("#progressBar").width() * percent); | |
$("#progressBarInner").css("width", width + "px"); | |
} | |
}, false); | |
xhrctrl.addEventListener("progress", function(event) { | |
if( event.lengthComputable ) { | |
console.log(event); | |
console.log( "upload2:" + event.loaded / event.total ); | |
} | |
}, false); | |
return xhrctrl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#sendButton").click(function() { | |
var formData = new FormData(); | |
formData.append("album", $("#album").val()); | |
var selectedFiles = $("#file")[0].files; | |
for (var i = 0; i < selectedFiles.length; i++) { | |
formData.append("image_" + i, selectedFiles[i]); | |
} | |
formData.append('_token', $('meta[name=csrf-token]').attr('content')); | |
formData.append('img_number', selectedFiles.length); | |
$.ajax({ | |
url: "/image", | |
method: "post", | |
processData: false, | |
data: formData, | |
contentType: false, | |
success: successHandler, | |
complete: completeHandler, | |
error: errorHandler, | |
xhr: getXHR | |
}) | |
.done(function() { | |
setInterval(reloadPage('/gallery'), 2000); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment