Skip to content

Instantly share code, notes, and snippets.

@dsdstudio
Created December 22, 2015 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsdstudio/a239cab218fd3c267261 to your computer and use it in GitHub Desktop.
Save dsdstudio/a239cab218fd3c267261 to your computer and use it in GitHub Desktop.
file upload lib prototyping
function upload(opt) {
opt = $.extend({
method:'POST',
url:'',
data:{},
onprogress:function() {},
onload:function(e, file) {},
onsuccess:function(e, file) {},
onerror:function(e, file) {}
}, opt);
var xhr = new XMLHttpRequest();
xhr.open(opt.method, opt.url);
// native Form Data 세팅
var form = new FormData();
for ( var k in opt.data) form.append(k, opt.data[k]);
xhr.upload.onprogress = opt.onprogress;
xhr.upload.onload = function(e) {
opt.onload.apply(null, [e, opt.data.file]);
};
xhr.onreadystatechange = function (e) {
if (e.currentTarget.readyState == 4) {
if (e.currentTarget.status == 200) opt.onsuccess.apply(null, [e.currentTarget, opt.data.file]);
else opt.onerror.apply(null, [e.currentTarget, opt.data.file]);
}
};
xhr.send(form);
return xhr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment