Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Forked from phpfour/upload.js
Created October 20, 2012 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajaxray/3924745 to your computer and use it in GitHub Desktop.
Save ajaxray/3924745 to your computer and use it in GitHub Desktop.
HTML5 Upload
(function () {
var input = document.getElementById("image"), formdata = false;
if (window.FormData) {
formdata = new FormData();
}
input.addEventListener("change", function (evt) {
file = this.files[0];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
//showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("image", file);
}
}
if (formdata) {
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
window.uploadedImage = res;
$('#image-holder').append('<img src="' + window.uploadedImage + '" />');
}
});
}
}, false);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment