Created
December 22, 2015 06:02
-
-
Save dsdstudio/a239cab218fd3c267261 to your computer and use it in GitHub Desktop.
file upload lib prototyping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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