woguupload.js官方教程
描述:html5上传程序[支持断点续传],支持chrome,firefox,ie10
作者:wogu(张文博)
qq:88433062
版本:1.0
版权:免费
woguupload的选项:
persize: 每次发送的字节数
formid: 要绑定的表单id
id: file元素的id。如果没有指定formid,则file元素的onchange事件发生后,自动开始上传
url: socket服务端地址
parator: 命令的分隔符,默认是”$$##$$”
filenamepre: 文件前缀。典型的,可以是用户id,以区分不同用户上传的文件
woguupload的事件:
onprocess(ndsize, size): ndsize为已发送字节数,size为总字节数。可以用此事件完成进度显示
oncomplete(size): size为总字节数,上传完成时调用
onopen: socket打开时调用
onclo: socket关闭时调用,发生于oncomplete之后
调用示例:
[html]
<!doctype html>
<html>
<head>
<title>file</title>
<meta http-equiv=”content-type” content=”text/html; chart=utf-8″ />
<script type=”text/javascript” src=”woguupload.js”></script>
</head>
<body>
<form id=”myform”>
<input id=”myfile” type=”file”/>
<input type=”submit” value=”upload”>
</form>
<span id=”tip”></span>
</body>
</html>
<script type=”text/javascriptR关于端午节手抄报内容21;>
var tip = document.getelementb象限是什么意思yid(‘tip’);
var wgup = new woguupload({
‘id’ : ‘myfile’,
‘formid’ : ‘myform’,
‘url’ : ‘ws://www.test.com:9300’,
‘onprocess’ : function(ndsize, size) {
document.getelementbyid(‘tip’).innerhtml = ndsize+’/’+size;
},
‘oncomplete’ : function(size) {
document.getelementbyid(‘tip’).innerhtml = size+’/’+size;
alert(‘上传完成!’);
},
‘onopen’ : function() {
tip.innerhtml = ‘onopen’;
},
‘onclo’ : function() {
tip.innerhtml = ‘onclo’;
}
});
</script>
woguupload.js源代码
[javascript]
/**
* html5上传程序[支持断点续传],支持chrome,firefox,ie10
*
* author: wogu(张文博)
* version: 1.0
* copyright: free
* document:
* lastmodify: 2012-8-7
*/
var woguupload = function(options) {
var file,persize,sock,size,s波波头适合什么脸型endsize,end,fr,parator,filenamepre;
var init = function(instance) {
fr = new filereader();
parator = options.parator || ‘$$##$$’;
filenamepre = options.filenamepre || ‘woguupload’;
size = ndsize = end = 0;
persize = options.persize || 1024*50/*50k*/;
try {
sock = woguwebsocket(options.url);
sock.onopen = function() {
if(options.onopen) {
options.onopen();
}
}
sock.onmessage = function(event) {
var cmd = event.data.split(parator);
if(‘0’ == cmd[0]) {
ndsize = parint(cmd[1]);
doupload();
} el if(‘1’ == cmd[0]) {
//服务器真实写入的数据
var realwrite = parint(cmd[1]);
//如果写入失败则重新发送
if(realwrite == 0) {
doupload();
return;
}
if(end < size) {
ndsize = end;
if(options.onprocess) {
options.onprocess(ndsize, size);
}
doupload();
} el {
if(options.oncomplete) {
options.oncomplete(size);
sock.clo();
}
}
}
}
sock.onclo = function() {
if(options.onclo) {
options.onclo();
}
}
} catch(e) {
alert(e);
}
var elem = document.getelementbyid(options.id);
if(options.formid) {
var form = document.getelementbyid(options.formid);
if(form) {
眼泪簌簌 form.onsubmit = bind(instance, function(event) {
upload();
return fal;
});
}
}
elem.onchange = bind(instance, function() {
file = elem.files[0];
if(!form) {
upload();
}
});
}
var woguslice = function(file, start, end, contenttype) {
if(file.mozslice) {
return file.mozslice(start, end, contenttype);
} el if(file.webkitslice) {
return file.webkitslice(start, end, contenttype);
}
}
var woguwebsocket = function(url) {
if(window.websocket) {
return new websocket(url);
} el if(window.mozwebsocket) {
return new mozwebsocket(url);
}
}
var doupload = function() {
end = ndsize + persize;
end = end > size ? size : end;
var blob = woguslice(file, ndsize, end);
fr.readasarraybuffer(blob);
fr.onloadend = function() {
sock.nd(fr.result);
}
}
var getfilename = function(file) {
var filename = filenamepre + ‘-‘ + file.size;
if(file.lastmodifieddate) {
var dateinfo = file.lastmodifieddate.tostring().split(‘ ‘);
var date = dateinfo[1] + dateinfo[2] + dateinfo[3] + dateinfo[4];
date = date.replace(/\:/g, ”);
filename += ‘-‘ + date;
}
filename += ‘-‘ + file.name;
return filename;
}
var upload = function() {
if(!file) {
return;
}
size = file.size;
var filename = getfilename(file);
var cmd = getcmd([0, filename]);
sock.nd(cmd);
}
var bind = function(obj, func) {
return function() {
return func.call(obj);
}
}
v9年级上册数学ar getcmd = function(args) {
return args.join(parator);
}
www.2cto.com
init(this);
}
作者:xiaodao1986
本文发布于:2023-04-03 01:36:14,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c3d0677da7e3383c71f020b6ebd42f55.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:HTML5断点续传.doc
本文 PDF 下载地址:HTML5断点续传.pdf
留言与评论(共有 0 条评论) |