Skip to content

Instantly share code, notes, and snippets.

@emayk
Forked from fredkingham/gist:4058933
Created June 12, 2013 14:26
Show Gist options
  • Select an option

  • Save emayk/5765746 to your computer and use it in GitHub Desktop.

Select an option

Save emayk/5765746 to your computer and use it in GitHub Desktop.
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