jQuery+ajax實現(xiàn)文件上傳功能
jQuery+ajax實現(xiàn)文件上傳功能(顯示文件上傳進度),供大家參考,具體內(nèi)容如下
具體實現(xiàn)步驟
1、定義UI結(jié)構(gòu),引入bootstrap的CSS文件和jQuery文件
2、給上傳按鈕綁定點擊事件
3、驗證是否選擇了文件
4、向FormData中追加文件
5、使用ajax發(fā)起上傳文件的請求
6、設置文件的路徑
7、使用xhr獲得文件上傳的進度
8、當文件上傳完成讓進度條顯示綠色
<style>
#loading {
width: 20px;
height: 20px;
}
#img {
display: block;
width: 200px;
height: 200px;
border-radius: 50%;
background-color: #abcdef;
opacity: .5;
}
</style>
<body>
<!--multiple可以選擇多個文件 -->
<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>
<!-- 顯示上傳到服務器的圖片 -->
<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('請上傳文件')
}
// 創(chuàng)建formdata
var fd = new FormData()
// 向formdata中傳入數(shù)據(jù)
// fd.append()
// file是一個偽數(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ù)對象不需要轉(zhuǎn)換成鍵值對格式
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()
// 獲取文件上傳的進度
xhr.upload.onprogress = function(e) {
// e.lengthComputable表示當前的進度是否是可以計算,返回布爾值
if (e.lengthComputable) {
// e.loaded表示下載了多少數(shù)據(jù), e.total表示數(shù)據(jù)總量
var percentComplete = Math.ceil((e.loaded / e.total) * 100)
// 讓進度條的寬度變化
$('#progress').css('width', percentComplete)
// 在進度條中顯示百分比
$('#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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript jQuery 中定義數(shù)組與操作及jquery數(shù)組操作
這篇文章主要介紹了JavaScript jQuery 中定義數(shù)組與操作及jquery數(shù)組操作的相關(guān)資料,需要的朋友可以參考下2015-12-12
jquery設置元素的readonly和disabled的寫法
Jquery的api中提供了對元素應用disabled和readonly屬性的方法,在這里記錄下,感興趣的朋友可以練練手了2013-09-09
jQuery表單驗證插件formValidator(改進版)
隨著jQuery被越來越多的人使用,基于jQuery的表單驗證插件,也從無到現(xiàn)在比較流行的已經(jīng)有10個左右了2012-02-02
Asp.net下使用Jquery Ajax傳送和接收DataTable的代碼
對于習慣使用GridView的人來說,前臺頁面需要動態(tài)添加表格的行數(shù),是一件痛苦的事。GridView處理這種事情相當麻煩,你點擊“新增一行”,需要回傳到服務器。2010-09-09
jquery插件之信息彈出框showInfoDialog(成功/錯誤/警告/通知/背景遮罩)
某某同學最近寫了個基于jquery的信息彈出插件showInfoDialog,該插件對背景進行遮罩,然后彈出信息顯示框,信息顯示種類包括(操作成功/錯誤信息/警告信息/通知信息)感興趣的朋友可以了解下2013-01-01

