jQuery+ajax實(shí)現(xiàn)文件上傳功能
jQuery+ajax實(shí)現(xiàn)文件上傳功能(顯示文件上傳進(jìn)度),供大家參考,具體內(nèi)容如下
具體實(shí)現(xiàn)步驟
1、定義UI結(jié)構(gòu),引入bootstrap的CSS文件和jQuery文件
2、給上傳按鈕綁定點(diǎn)擊事件
3、驗(yàn)證是否選擇了文件
4、向FormData中追加文件
5、使用ajax發(fā)起上傳文件的請(qǐng)求
6、設(shè)置文件的路徑
7、使用xhr獲得文件上傳的進(jìn)度
8、當(dāng)文件上傳完成讓進(jìn)度條顯示綠色
<style>
#loading {
width: 20px;
height: 20px;
}
#img {
display: block;
width: 200px;
height: 200px;
border-radius: 50%;
background-color: #abcdef;
opacity: .5;
}
</style>
<body>
<!--multiple可以選擇多個(gè)文件 -->
<input type="file" multiple name="" id="ipt" multiple><button id="btn" type="submit">上傳文件</button>
<img id="loading" src="../img/loading.gif" alt="" style="display: none;">
<!-- bootstrap中引入條件 -->
<div class="progress" style="margin-top: 10px;width: 100px;margin-left: 10px;">
<div id="progress" class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="45" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">
0%
</div>
</div>
<!-- 顯示上傳到服務(wù)器的圖片 -->
<img src="" alt="" id="img" style="display: none;">
<script src="../lib/jquery-1.11.0.min.js"></script>
<script>
$(function() {
$('#btn').on('click', function() {
// 獲取文件列表
var file = $('#ipt')[0].files
// 判斷是否選擇了文件
if (file.length <= 0) {
return alert('請(qǐng)上傳文件')
}
// 創(chuàng)建formdata
var fd = new FormData()
// 向formdata中傳入數(shù)據(jù)
// fd.append()
// file是一個(gè)偽數(shù)組
fd.append('avatar', file[0])
// 用ajax傳送數(shù)據(jù)
$.ajax({
type: 'post',
url: 'http://www.liulongbin.top:3006/api/upload/avatar',
// 數(shù)據(jù)不需要編碼
contentType: false,
// 數(shù)據(jù)對(duì)象不需要轉(zhuǎn)換成鍵值對(duì)格式
processData: false,
data: fd,
beforeSend: function() {
$('#loading').show()
},
complete: function() {
$('#loading').hide()
},
success: function(res) {
// 判斷是否接收成功
if (res.status !== 200) {
return alert(reg.msg)
}
$('#img').attr('src', 'http://www.liulongbin.top:3006' + res['url']).css('display', 'block')
},
xhr: function xhr() {
var xhr = new XMLHttpRequest()
// 獲取文件上傳的進(jìn)度
xhr.upload.onprogress = function(e) {
// e.lengthComputable表示當(dāng)前的進(jìn)度是否是可以計(jì)算,返回布爾值
if (e.lengthComputable) {
// e.loaded表示下載了多少數(shù)據(jù), e.total表示數(shù)據(jù)總量
var percentComplete = Math.ceil((e.loaded / e.total) * 100)
// 讓進(jìn)度條的寬度變化
$('#progress').css('width', percentComplete)
// 在進(jìn)度條中顯示百分比
$('#progress').html(percentComplete + 'px')
}
}
// 文件加載完成
xhr.upload.onload = function() {
$('#progress').removeClass('progress-bar progress-bar-striped').addClass('progress-bar progress-bar-success')
}
return xhr
}
})
})
})
</script>
</body>
效果演示(slow3g狀態(tài))


以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery Ajax文件上傳(php)
- jquery ajax實(shí)現(xiàn)文件上傳功能實(shí)例代碼
- 基于HTML5 Ajax文件上傳進(jìn)度條如何實(shí)現(xiàn)(jquery版本)
- jquery插件ajaxupload實(shí)現(xiàn)文件上傳操作
- jQuery+ajax簡單實(shí)現(xiàn)文件上傳的方法
- jQuery插件AjaxFileUpload實(shí)現(xiàn)ajax文件上傳
- 基于jquery ajax的多文件上傳進(jìn)度條過程解析
- jquery實(shí)現(xiàn)異步文件上傳ajaxfileupload.js
- jquery?ajax實(shí)現(xiàn)文件上傳提交的實(shí)戰(zhàn)步驟
相關(guān)文章
jQuery實(shí)現(xiàn)的超酷蘋果風(fēng)格圖標(biāo)滑出菜單效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)的超酷蘋果風(fēng)格圖標(biāo)滑出菜單效果代碼,涉及jQuery基于鼠標(biāo)hover事件動(dòng)態(tài)操作頁面元素屬性的相關(guān)技巧,非常美觀實(shí)用,需要的朋友可以參考下2015-09-09
JQuery操作表格(隔行著色,高亮顯示,篩選數(shù)據(jù))
最近項(xiàng)目里對(duì)表格的操作比較多。以往我們要做一些效果的時(shí)候往往通過程序代碼來實(shí)現(xiàn),這個(gè)努力不值,因?yàn)镴Query是完全可以做到的,并且是客戶端運(yùn)行,不經(jīng)過服務(wù)器處理,給用戶的反應(yīng)快,也減少了服務(wù)器壓力2012-02-02
Jquery知識(shí)點(diǎn)二 jquery下對(duì)數(shù)組的操作
眾所周知,Jquery是對(duì)JavaScript的一種高效的封裝,所以Jquery要操作的數(shù)組即是JavaScript中的數(shù)組,在JavaScript中我們使用for以及for-in進(jìn)行數(shù)組的操作,而在Jquery中則使用$.map()、$.each()來操作數(shù)組2011-01-01
使用基于jquery的gamequery插件做JS乒乓球游戲
現(xiàn)在jquery比較流行,用js做游戲的也越來越多了,雖然現(xiàn)在html5出來了,但實(shí)際上要用html5做點(diǎn)啥出來還是得靠javascript,所以學(xué)好js是非常重要的2011-07-07
JQuery DataTable刪除行后的頁面更新利用Ajax解決
使用Jquery的DataTable進(jìn)行數(shù)據(jù)表處理非常方便,常遇到的一個(gè)問題就是刪除一行后頁面必須進(jìn)行更新,下面與大家分享下使用Ajax的解決方法2013-05-05
jQuery實(shí)現(xiàn)的超簡單輪播圖功能示例【代碼解釋】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的超簡單輪播圖功能,結(jié)合完整實(shí)例形式分析了基于jQuery實(shí)現(xiàn)的輪播圖相關(guān)功能實(shí)現(xiàn)、樣式設(shè)置與注意事項(xiàng),需要的朋友可以參考下2023-05-05

