踩坑視頻上傳:
點擊開始上傳:
頭部引入webuploader.css
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="css/webuploader.css">
</head>
<body>
<div id="uploadfile">
<div id="picker" >選擇文件</div>
<!--用來存放文件信息-->
<div id="thelist" class="uploader-list">
<table class="table" border="1" cellpadding="0" cellspacing="0" width="100%">
<tr class="filelist-head">
<th width="5%" class="file-num">序號</th>
<th class="file-name">視頻名稱</th>
<th class="file-size">大小</th>
<th width="20%" class="file-pro">進度</th>
<th class="file-status">狀態</th>
<th width="20%" class="file-manage">操作</th>
</tr>
</table>
</div>
<div id="ctlBtn" class="btn btn-default">開始上傳</div>
</div>
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/webuploader.min.js"></script>
<script>
$(function(){
//視頻上傳 start
var $list = $('#thelist .table'),
$btn = $('#ctlBtn');
var uploader = WebUploader.create({
resize: false, // 不壓縮image
swf: '../js/uploader.swf', // swf文件路徑
server: 'upload.php', // 文件接收服務端。
pick: '#picker', // 選擇文件的按鈕。可選
chunked: true, //是否要分片處理大文件上傳
chunkSize:2*1024*1024, //分片上傳,每片2M,默認是5M
// auto: true, //選擇文件后是否自動上傳
// chunkRetry : 2, //如果某個分片由於網絡問題出錯,允許自動重傳次數
//runtimeOrder: 'html5,flash',
// accept: {
// title: 'Images',
// extensions: 'gif,jpg,jpeg,bmp,png',
// mimeTypes: 'image/*'
// }
duplicate: false //是否支持重復上傳
});
// 當有文件被添加進隊列的時候
uploader.on( 'fileQueued', function( file ) {
$list.append('<tr id="'+ file.id +'" class="file-item">'+'<td width="5%" class="file-num">111</td>'+'<td class="file-name">'+ file.name +'</td>'+ '<td width="20%" class="file-size">'+ (file.size/1024/1024).toFixed(1)+'M' +'</td>' +'<td width="20%" class="file-pro">0%</td>'+'<td class="file-status">等待上傳</td>'+'<td width="20%" class="file-manage"><a class="stop-btn" href="javascript:;">暫停</a><a class="remove-this" href="javascript:;">取消</a></td>'+'</tr>');
//暫停上傳的文件
$list.on('click','.stop-btn',function(){
uploader.stop(true);
})
//刪除上傳的文件
$list.on('click','.remove-this',function(){
if ($(this).parents(".file-item").attr('id') == file.id) {
uploader.removeFile(file);
$(this).parents(".file-item").remove();
}
})
});
//重復添加文件
var timer1;
uploader.onError = function(code){
clearTimeout(timer1);
timer1 = setTimeout(function(){
layer.msg('該文件已存在');
},250);
}
// 文件上傳過程中創建進度條實時顯示
uploader.on( 'uploadProgress', function( file, percentage ) {
$("td.file-pro").text("");
var $li = $( '#'+file.id ).find('.file-pro'),
$percent = $li.find('.file-progress .progress-bar');
// 避免重復創建
if ( !$percent.length ) {
$percent = $('<div class="file-progress progress-striped active">' +
'<div class="progress-bar" role="progressbar" style="width: 0%">' +
'</div>' +
'</div>' + '<br/><div class="per">0%</div>').appendTo( $li ).find('.progress-bar');
}
$li.siblings('.file-status').text('上傳中');
$li.find('.per').text((percentage * 100).toFixed(2) + '%');
$percent.css( 'width', percentage * 100 + '%' );
});
// 文件上傳成功
uploader.on( 'uploadSuccess', function( file ) {
$( '#'+file.id ).find('.file-status').text('已上傳');
});
// 文件上傳失敗,顯示上傳出錯
uploader.on( 'uploadError', function( file ) {
$( '#'+file.id ).find('.file-status').text('上傳出錯');
});
// 完成上傳完后將視頻添加到視頻列表,顯示狀態為:轉碼中
uploader.on( 'uploadComplete', function( file ) {
// $( '#'+file.id ).find('.file-progress').fadeOut();
});
$btn.on('click', function () {
if ($(this).hasClass('disabled')) {
return false;
}
uploader.upload();
});
})
</script>
</body>
</html>
最好是在服務器環境中運行,就能看出效果了
另外關於***文件重復上傳***提示,參考http://c7.gg/fw4sn,感覺這個寫的很全面了。