帶進度條上傳多視訊
阿新 • • 發佈:2019-02-03
效果:
引用:
<link rel="stylesheet" href="bootstrap.css">
<script src="jquery.fileupload.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
html:
<div class="form-group">
<label>產品視訊:</label>
<div class="videoUpfile">
<input type="file" name="avatarVideo" class="avatarVideo"><a class="btn deleteAvatarVideo" href="javascript:;">刪除</a>
<div class="videoMaterials">
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
<span class="sr-only">0% Complete</span>
</div>
</div >
<div class="files"></div>
<div class="showimg"></div>
<input type="hidden" value="" name="video_id[]">
<p style="color: #b92c28" class="img_upload_info"></p>
</div>
</div>
<div class="videoUpfile">
<input type="file" name="avatarVideo" class="avatarVideo"><a class="btn deleteAvatarVideo" href="javascript:;">刪除</a>
<div class="videoMaterials">
<div class="progress">
<div class="progress-bar progress-bar-success progress-bar-striped" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
<span class="sr-only">0% Complete</span>
</div>
</div>
<div class="files"></div>
<div class="showimg"></div>
<input type="hidden" value="" name="video_id[]">
<p style="color: #b92c28" class="img_upload_info"></p>
</div>
</div>
</div>
script:
//上傳視訊
$(".avatarVideo").change(function() {
var _this = $(this);
_this.wrap("<form class='fileUploadeVideo' action='/admin/uploadimg/addVideo' method='post' enctype='multipart/form-data'></form>");
_this.parents('.fileUploadeVideo').ajaxSubmit({
dataType: 'json',
beforeSend: function () {
$(".progress").show();
},
uploadProgress: function (event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
_this.parents('.videoUpfile').find(".progress-bar").width(percentComplete + '%');
_this.parents('.videoUpfile').find(".progress-bar").html(percentVal);
_this.parents('.videoUpfile').find(".sr-only").html(percentComplete + '%');
},
success: function (data) {
if(data.code==100) {
_this.parents('.videoUpfile').find(".files").html("檔名: " + data.video_title);
_this.parents('.videoUpfile').find("input[type=hidden]").val(data.video_id);
alert("上傳成功!");
}else{
alert("上傳失敗");
}
},
error: function () {
alert("上傳失敗");
}
});