;(function(window,document){
var myUpload = function(option) {
var file,
fd = new FormData(),
xhr = new XMLHttpRequest(),
loaded, tot, per, uploadUrl, input;
input = document.getElementById(option.inputID);
uploadUrl = option.uploadUrl;
callback = option.callback;
beforeSend = option.beforeSend;
file = input.files[0];
if(beforeSend instanceof Function){
if(beforeSend(file) === false){
return false;
}
}
fd.append("files", file);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
if(callback instanceof Function){
callback(xhr.responseText);
}
}
}
xhr.open("post", uploadUrl);
xhr.send(fd);
};
window.myUpload = myUpload;
})(window,document);
//调用方法
//假设选择文件input的id = 'inputFileUpload', 上传按钮id = 'upload'
//请求链接为test.com
$('#upload').click(function(){
inputID: 'inputFileUpload',
uploadUrl: 'test.com',
beforeSend: function(file){
//文件上传前的操作
},
callback: function(res){
//回调函数,参数为请求返回内容
}
});