百度开源上传组件webuploader 可上传多文件并带有进度条
生活随笔
收集整理的這篇文章主要介紹了
百度开源上传组件webuploader 可上传多文件并带有进度条
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
//上傳多文件
function UploadMultiFile() {
var uploader = WebUploader.create({
// 選完文件后,是否自動上傳。
auto: true,
// swf文件路徑
swf: '~/Content/libs/webuploader/Uploader.swf',
// 文件接收服務端。
server: '../../Document/Art/UploadFile',
// 選擇文件的按鈕。可選。
// 內(nèi)部根據(jù)當前運行是創(chuàng)建,可能是input元素,也可能是flash.
pick: '#picker',
// 不壓縮image, 默認如果是jpeg,文件上傳前會壓縮一把再上傳!
resize: false
});
// 當有文件被添加進隊列的時候
uploader.on('fileQueued', function (file) {
var itemTemplate = "";
var fileSize = parseInt(file.size / 1024);
if (fileSize >= 1024)
fileSize = parseInt(fileSize / 1024) + "MB"
else
fileSize = parseInt(fileSize) + "KB"
itemTemplate = '<div id="' + file.id + '" class="uploadify-queue-item">
<div><img src="/Content/img/filetype/'+%20file.ext%20+%20'.png"></div>
<div class="cancel">
<a href=""></a>
</div>
<span class="fileName">'+ file.name + ' (' + fileSize + ')</span><span class="data"></span>
</div>'+ itemTemplate;
$(".border").prepend(itemTemplate);
$(".drag-tip").remove();
});
// 文件上傳過程中創(chuàng)建進度條實時顯示。
uploader.on('uploadProgress', function (file, percentage) {
var $li = $('#' + file.id),
$percent = $li.find('.uploadify-progress .uploadify-progress-bar');
// 避免重復創(chuàng)建
if (!$percent.length) {
$percent = $('<div class="uploadify-progress uploadify-progress-striped active">' +
'<div class="uploadify-progress-bar" role="progressbar">' +
'</div>' +
'</div>').appendTo($li).find('.uploadify-progress-bar');
}
$li.find('.data').text(' 上傳中').css("color", "red");;
$percent.css('width', percentage * 100 + '%');
});
//文件成功、失敗處理
uploader.on('uploadSuccess', function (file) {
$('#' + file.id).find('.data').text(' 上傳成功').css("color","green");
});
uploader.on('uploadError', function (file) {
$('#' + file.id).find('.data').text(' 上傳出錯');
});
uploader.on('uploadComplete', function (file) {
$('#' + file.id).find('.uploadify-progress').fadeOut();
});
}
<div>
<div id="uploader" class="wu-example">
<!--用來存放文件信息-->
<div id="fileList" class="uploader-list"></div>
<div class="btns">
<div id="picker">選擇文件</div>
</div>
</div>
</div>
<div class="border">
<div class="drag-tip">
<h1>您的瀏覽器不支持拖拽功能,請點擊按鈕上傳。</h1>
<p>
(您的瀏覽器不支持此拖拽功能)
</p>
</div>
</div>
注意:使用Web Uploader文件上傳需要引入三種資源:JS, CSS, SWF
<!--引入CSS--> <link rel="stylesheet" type="text/css" href="webuploader文件夾/webuploader.css"> <!--引入JS--> <script type="text/javascript" src="webuploader文件夾/webuploader.js"></script>
補充上傳成功后返回值的兩方結(jié)合的問題:
uploader.on('uploadSuccess', function (file, response) {
if (response.type == 1 && response.errorcode == 0) {
var arrData = [];
var sort = 0;
var inputValue = $("#fileData").val();
if (inputValue != "") {
var jsonObj = JSON.parse(inputValue);
for (var i = 0; i < jsonObj.length; i++) {
sort = i + 1;
jsonObj[i]["SortCode"] = sort;
arrData.push(jsonObj[i]);
}
}
var newAdd = {};
newAdd["FileName"] = file.name;
newAdd["FilePath"] = response.message;
newAdd["FileSize"] = parseInt(file.size / 1024);
newAdd["FileExtensions"] = file.ext;
newAdd["FileType"] = file.type;
newAdd["SortCode"] = sort + 1;
arrData.push(newAdd);
$("#fileData").val(JSON.stringify(arrData));
}
});
總結(jié)
以上是生活随笔為你收集整理的百度开源上传组件webuploader 可上传多文件并带有进度条的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sublime text 之snippe
- 下一篇: nodeType