Ajax實(shí)現(xiàn)上傳圖像功能的示例詳解
最終效果展示
xhr發(fā)起請(qǐng)求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" rel="external nofollow" > <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <!--1、文件選擇框 --> <input type="file" id="file1"> <!-- 2、上傳文件的按鈕 --> <button id="btnupload">上傳文件</button> <br/> <div class="progress" style="width:300px;margin:5px;"> <div class="progress-bar progress-bar-striped active" style="width: 0%;" id="precent"> 0% </div> </div> <!-- 3、img標(biāo)簽 來(lái)顯示上傳成功以后的圖片 --> <img alt="" id="img" width="800"> <script> var precent = document.querySelector('#precent') var btnupload = document.querySelector('#btnupload') btnupload.addEventListener('click', function() { var files = document.querySelector('#file1').files if (files.length <= 0) { return alert('請(qǐng)選擇要上傳的文件') } var fd = new FormData() fd.append('avatar', files[0]) var xhr = new XMLHttpRequest() xhr.upload.onprogress = function(e) { console.log(e); if (e.lengthComputable) { var h = Math.ceil((e.loaded / e.total) * 100) precent.style.width = h + '%' precent.innerHTML = h + '%' console.log(h); } } xhr.upload.onload = function() { $('#precent').removeClass().addClass('progress-bar progress-bar-success') } xhr.open('post', 'http://www.liulongbin.top:3006/api/upload/avatar') xhr.send(fd) xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { var data = JSON.parse(xhr.responseText) console.log(data); if (data.status == 200) { console.log('上傳成功'); document.querySelector('#img').src = 'http://www.liulongbin.top:3006' + data.url } else { console.log('上傳失敗'); } } } }) </script> </body> </html>
ajax發(fā)起請(qǐng)求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="jquery.js"></script> <style> img { width: 50px; height: 50px; display: none; } </style> </head> <body> <input type="file" id="file1"> <button id="btnupload">上傳文件</button> </br> <img src="5-121204193R5-50.gif" alt="" id="loading"> <script> $(function() { $(document).ajaxStart(function() { $('#loading').show() }) $(document).ajaxStop(function() { $('#loading').hide() }) $('#btnupload').on('click', function() { var files = $('#file1')[0].files if (files.length <= 0) { alert('請(qǐng)選擇文件') } console.log('ok'); var fd = new FormData() fd.append('avatar', files[0]) $.ajax({ method: 'POST', url: 'http://www.liulongbin.top:3006/api/upload/avatar', data: fd, processData: false, contentType: false, success: function(res) { console.log(res); } }) }) }) </script> </body> </html>
到此這篇關(guān)于Ajax實(shí)現(xiàn)上傳圖像功能的示例詳解的文章就介紹到這了,更多相關(guān)Ajax上傳圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ajaxrequest.js ajaxrequest 0.7最新版 使用AJAXRequest進(jìn)行AJAX應(yīng)用程序開發(fā)入
ajaxrequest.js ajaxrequest 0.7最新版 使用AJAXRequest進(jìn)行AJAX應(yīng)用程序開發(fā)入門小技巧...2007-12-12ajax實(shí)現(xiàn)省市三級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了ajax實(shí)現(xiàn)省市三級(jí)聯(lián)動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12發(fā)現(xiàn)個(gè)AJAX圖片瀏覽器SIMPLEVIEWER
發(fā)現(xiàn)個(gè)AJAX圖片瀏覽器SIMPLEVIEWER...2007-04-04Spring MVC前端與后端5種ajax交互方法【總結(jié)】
下面小編就為大家?guī)?lái)一篇Spring MVC前端與后端5種ajax交互方法【總結(jié)】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Ajax通過(guò)XML異步提交的方法實(shí)現(xiàn)從數(shù)據(jù)庫(kù)獲取省份和城市信息實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)(xml方法)
這篇文章主要介紹了Ajax通過(guò)XML異步提交的方法實(shí)現(xiàn)從數(shù)據(jù)庫(kù)獲取省份和城市信息實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)(xml方法)的相關(guān)資料,我們要根據(jù)異步提交,局部刷新的思想來(lái)實(shí)現(xiàn)來(lái)提高用戶交互問(wèn)題,對(duì)ajax二級(jí)聯(lián)動(dòng)效果感興趣的朋友一起看看吧2016-11-11django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤如何解決
在django中,使用jquery ajax post數(shù)據(jù),會(huì)出現(xiàn)403的錯(cuò)誤,該如何解決呢?下面由腳本之家小編幫大家解決django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤,需要的朋友可以參考下2015-09-09AJAX入門之深入理解JavaScript中的函數(shù)
AJAX入門之深入理解JavaScript中的函數(shù)...2007-02-02PHP Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新發(fā)表評(píng)論
PHP Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新發(fā)表評(píng)論...2006-12-12