当前位置:编程学习 > html/css >>

HTML5断点续传

WoguUpload.js官方教程

描述:HTML5上传程序[支持断点续传],支持chrome,firefox,ie10
作者:wogu(张文博)
QQ:88433062
版本:1.0
版权:免费

WoguUpload的选项:
persize: 每次发送的字节数
formid: 要绑定的表单ID
id: file元素的ID。如果没有指定formid,则file元素的onchange事件发生后,自动开始上传
url: socket服务端地址
separator: 命令的分隔符,默认是"$$##$$"
filenamepre: 文件前缀。典型的,可以是用户ID,以区分不同用户上传的文件

WoguUpload的事件:
onProcess(sendsize, size): sendsize为已发送字节数,size为总字节数。可以用此事件完成进度显示
onComplete(size): size为总字节数,上传完成时调用
onOpen: socket打开时调用
onClose: socket关闭时调用,发生于onComplete之后

调用示例:
[html] 
<!DOCTYPE HTML> 
<html> 
 
<head> 
    <title>file</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=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/javascript"> 
var tip = document.getElementById('tip'); 
var wgup = new WoguUpload({ 
    'id' : 'myfile', 
    'formid' : 'myform', 
    'url' : 'ws://www.test.com:9300', 
    'onProcess' : function(sendsize, size) { 
        document.getElementById('tip').innerHTML = sendsize+'/'+size; 
    }, 
    'onComplete' : function(size) { 
        document.getElementById('tip').innerHTML = size+'/'+size; 
        alert('上传完成!'); 
    }, 
    'onOpen' : function() { 
        tip.innerHTML = 'onopen'; 
    }, 
    'onClose' : function() { 
        tip.innerHTML = 'onclose'; 
    } 
}); 
</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,sendsize,end,fr,separator,filenamepre; 
     
    var init = function(instance) { 
        fr = new FileReader(); 
        separator = options.separator || '$$##$$'; 
        filenamepre = options.filenamepre || 'woguupload'; 
        size = sendsize = 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(separator); 
                if('0' == cmd[0]) { 
                    sendsize = parseInt(cmd[1]); 
                    doupload(); 
                } else if('1' == cmd[0]) { 
                    //服务器真实写入的数据 
                    var realwrite = parseInt(cmd[1]); 
 
                    //如果写入失败则重新发送 
                    if(realwrite == 0) { 
                        doupload(); 
                        return; 
                    } 
                     
                    if(end < size) { 
                        sendsize = end; 
                        if(options.onProcess) { 
                            options.onProcess(sendsize, size); 
                        } 
       

补充:web前端 , HTML 5 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,