jQuery+php+ajax实现无刷新上传文件功能
生活随笔
收集整理的這篇文章主要介紹了
jQuery+php+ajax实现无刷新上传文件功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
jQuery+php+ajax實(shí)現(xiàn)無刷新上傳文件功能,還帶有上傳進(jìn)度條動畫效果,支持圖片、視頻等大文件上傳。
js代碼:
<script type='text/javascript' src='js/jquery-2.0.3.min.js'></script> <script type='text/javascript' src='js/jquery.form.js'></script> <script type="text/javascript"> function filesize(ele) { var filesize = (ele.files[0].size / 1024/1024).toFixed(2); $('#big').html(filesize+"MB"); $('#text').html(ele.files[0].name); } $(document).ready(function(e) { var progress = $(".progress"); var progress_bar = $(".progress-bar"); var percent = $('.percent'); $("#del").click(function(){ var objFile=document.getElementsByTagName('input')[2]; objFile.value=""; $("#list").hide(); }); $("#uploadphoto").change(function(){ $("#list").show(); }); $("#ups").click(function(){ var file = $("#uploadphoto").val(); if(file!=""){ $('#uped').html("上傳中……"); $("#myupload").ajaxSubmit({ dataType: 'json', //數(shù)據(jù)格式為json beforeSend: function() { //開始上傳 var percentVal = '0%'; progress_bar.width(percentVal); percent.html(percentVal); }, uploadProgress: function(event, position, total, percentComplete) { var percentVal = percentComplete + '%'; //獲得進(jìn)度 progress_bar.width(percentVal); //上傳進(jìn)度條寬度變寬 percent.html(percentVal); //顯示上傳進(jìn)度百分比 }, success: function(data) { if(data.status == 1){ var src = data.url; $('#uped').html("上傳成功"); //var attstr= '<img src="'+src+'">'; //$(".imglist").append(attstr); //$(".res").html("上傳圖片"+data.name+"成功,圖片大小:"+data.size+"K,文件地址:"+data.url); }else{ $(".res").html(data.content); } }, error:function(xhr){ //上傳失敗 alert("上傳失敗"); } }); } else{ alert("請選擇視頻文件"); } }); }); </script>
upload.php源代碼:
?
轉(zhuǎn)載于:https://my.oschina.net/sucaihuocom/blog/3055825
總結(jié)
以上是生活随笔為你收集整理的jQuery+php+ajax实现无刷新上传文件功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git学习笔记与Intel
- 下一篇: ant-design如果按需加载组件