var OKUpload = function(){ this.action = "/_services/image" } OKUpload.prototype.bind = function(el){ if (el.length) el = el[0] el.addEventListener("change", this.handleFileSelect.bind(this)) } OKUpload.prototype.handleFileSelect = function(e) { e.stopPropagation(); e.preventDefault(); var files = e.dataTransfer ? e.dataTransfer.files : e.target.files; for (var i = 0, f; f = files[i]; i++) { if ( ! f.type.match('image.*')) { continue; } this.upload(f) } } OKUpload.prototype.upload = function(f){ var fd = new FormData() fd.append('image', f) var request = $.ajax({ url: this.action, type: "post", data: fd, dataType: "json", processData: false, contentType: false, }) request.done(this.success.bind(this)) } OKUpload.prototype.success = function(media){ if (media.error) { console.log(media.error) return } this.add(media) } OKUpload.prototype.add = function(media){ console.log(media) } OKUpload.prototype.error = function(error){ throw error }