Skip to content

Instantly share code, notes, and snippets.

@fredkingham
Created November 12, 2012 11:50
Show Gist options
  • Save fredkingham/4058933 to your computer and use it in GitHub Desktop.
Save fredkingham/4058933 to your computer and use it in GitHub Desktop.
ko-html5-image-upload
ko.bindingHandlers.fileUpload = {
update: function(element, valueAccessor, allBindingsAccessor){
var value = ko.utils.unwrapObservable(valueAccessor())
if(element.files.length && value){
var file = element.files[0];
var url = allBindingsAccessor().url
xhr = new XMLHttpRequest();
xhr.open("post", url, true);
xhr.setRequestHeader("Content-Type", "image/jpeg");
xhr.setRequestHeader("X-File-Name", file.name);
xhr.setRequestHeader("X-File-Size", file.size);
xhr.setRequestHeader("X-File-Type", file.type);
console.log("sending")
// Send the file (doh)
xhr.send(file);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment