jquery?ajax實(shí)現(xiàn)文件上傳提交的實(shí)戰(zhàn)步驟
JQuery實(shí)現(xiàn)文件上傳提交
定義UI結(jié)構(gòu)
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script> <input type="file" id="file1"> <button id="btnUpload">上傳文件</button> <img src="" alt="" style="width: 200px;" id="img1">
驗(yàn)證是否選擇了文件
$('#btnUpload').on('click', function () { let files = $('#file1')[0].files; if (files.length <= 0) { return alert('請選擇文件后在上傳') } })
向FormData中追加文件并發(fā)起ajax請求
//上傳文件 let fd = new FormData(); fd.append('avator', files[0]); //發(fā)起jquery ajax請求 $.ajax({ method: 'post', url: 'http://www.liulongbin.top:3006/api/upload/avatar', data: fd, processData: false, contentType: false, success: function (res) { alert('上傳成功') $('#img1').attr('src', 'http://www.liulongbin.top:3006' + res.url) console.log(res.url); } })
jquery實(shí)現(xiàn)loading效果
ajaxStart(callback)
Ajax請求開始時(shí),執(zhí)行ajaxStart函數(shù),可以在ajaxStart的callback中顯示loading效果。
自jqueyr版本1.8起,該方法只能被附加到文檔,$(document).ajaxStart()函數(shù)會(huì)監(jiān)聽文檔內(nèi)所有ajax請求,當(dāng)ajax請求開始會(huì)觸發(fā)這個(gè)函數(shù),ajax結(jié)束則會(huì)觸發(fā)ajaxStop
<img src="./自媒體資源/5-121204193933-51.gif" alt="" style="display: none;" id="loading" width="50px" height="50px"> $(document).ajaxStart(function () { $('#loading').show() }) $(document).ajaxStop(function () { $('#loading').hide() })
完整代碼
<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="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.js"></script> </head> <body> <input type="file" id="file1"> <button id="btnUpload">上傳文件</button> <br> <img src="" alt="" style="width: 200px;" id="img1"> <img src="./自媒體資源/5-121204193933-51.gif" alt="" style="display: none;" id="loading" width="50px" height="50px"> <script> //監(jiān)聽傳輸 $(document).ajaxStart(function () { $('#loading').show() }) $(document).ajaxStop(function () { $('#loading').hide() }) //建立單擊事件 $('#btnUpload').on('click', function () { let files = $('#file1')[0].files; if (files.length <= 0) { return alert('請選擇文件后在上傳') } //上傳文件 let fd = new FormData(); fd.append('avator', files[0]); //發(fā)起jquery ajax請求 $.ajax({ method: 'post', url: 'http://www.liulongbin.top:3006/api/upload/avatar', data: fd, processData: false, contentType: false, success: function (res) { alert('上傳成功') $('#img1').attr('src', 'http://www.liulongbin.top:3006' + res.url) console.log(res.url); } }) }) </script> </body>
補(bǔ)充:Jquery Ajax上傳文件并提交表單其它屬性
1.前端代碼
$('.btn-confirm').click(function () { console.log($( '#postForm').serialize()); var versionNo = $("#versionNo").val(); var deviceNoStr = $("input[name='deviceNoStr']").val(); var batchId = $("input[name='batchId']").val(); var formData = new FormData(); console.log($('#upfile')[0].files[0]); formData.append("upfile",$('#upfile')[0].files[0]); formData.append("versionNo", versionNo); formData.append("deviceNoStr", deviceNoStr); formData.append("batchId", batchId); $.ajax({ url :window.CMS_URL + "/devops/operation/upgrade.shtml", dataType:'json', type:'POST', async: false, data: formData, processData : false, // 使數(shù)據(jù)不做處理 contentType : false, // 不要設(shè)置Content-Type請求頭 success: function(data){ console.log(data); if (data.status == 'ok') { alert('上傳成功!'); } }, error:function(response){ console.log(response); } }); });
<form id= "uploadForm" method= "post" enctype ="multipart/form-data"> <div class="portlet-body" style="margin: 15px 0;"> <span class="file-des">程序路徑:</span> <div class="wrap-upload" style="display: flex"> <div class="showFileName" ></div> <a href="javascript:;" class="file btn btn-sm green">上傳文件 <input type="file" name="upfile" id="upfile" placeholder="請選擇文件"> </a> <div class="fileerrorTip" style="line-height: 30px;margin-right: 10px"></div> </div> </div> <div class="portlet-body"> <span class="file-des">版本號(hào):</span> <div class="wrap-upload item-inputs" > <input class="form-control" type="text" name="versionNo" id="versionNo" placeholder="版本號(hào)" required> </div> </div> <div style=" margin: 0px 100px;padding-top: 20px;"> <div class="btn btn-sm green control-btn-item btn-confirm" >確認(rèn)</div> <div style="margin-left: 30px;" class="btn btn-sm green control-btn-item" onclick="closePopup()">取消</div> </div> <input type="hidden" name="deviceNoStr" value="${(devopsParamsQo.deviceNoStr)!''}"/> <input type="hidden" name="batchId" value="${(devopsParamsQo.batchId)!''}"/> </form>
2.后端代碼
@RequestMapping(value = "/upgrade", method = RequestMethod.POST) @ResponseBody public ResultVo upgrade(@RequestPart( value = "upfile", required = true) MultipartFile file, @RequestParam(name = "versionNo", required = true) String versionNo, @RequestParam(name = "batchId", required = true) String batchId, @RequestParam(name = "deviceNoStr", required = true) String deviceNoStr) { DevopsParamsQo qo = new DevopsParamsQo(); qo.setDeviceNoStr(deviceNoStr); List<DeviceDto> addList = getDeviceDtos(qo); // TODO 調(diào)用前置 return ResultVoGenerator.genSuccessResultVo(); }
總結(jié)
到此這篇關(guān)于jquery ajax實(shí)現(xiàn)文件上傳提交的文章就介紹到這了,更多相關(guān)jquery ajax文件上傳提交內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- AJAX亂碼與異步同步以及封裝jQuery庫實(shí)現(xiàn)步驟詳解
- 實(shí)現(xiàn)AJAX異步調(diào)用和局部刷新的基本步驟
- js 實(shí)現(xiàn)ajax發(fā)送步驟過程詳解
- 利用AjaxControlToolkit實(shí)現(xiàn)百度搜索時(shí)的下拉列表提示詳細(xì)步驟
- AJAX的原理—如何做到異步和局部刷新【實(shí)現(xiàn)代碼】
- 談?wù)凙jax原理實(shí)現(xiàn)過程
- 利用iframe實(shí)現(xiàn)ajax跨域通信的實(shí)現(xiàn)原理(圖解)
- Ajax二級聯(lián)動(dòng)菜單實(shí)現(xiàn)原理及代碼
- js/ajax跨越訪問-jsonp的原理和實(shí)例(javascript和jquery實(shí)現(xiàn)代碼)
- Ajax實(shí)現(xiàn)步驟和原理解析
相關(guān)文章
innerHTML與jquery里的html()區(qū)別介紹
我原本一直以為innerHTML和jquery里的html其實(shí)是完全一樣的,jquery是多此一舉了,直到我遇到一次問題2012-10-10快速學(xué)習(xí)jQuery插件 jquery.validate.js表單驗(yàn)證插件使用方法
快速學(xué)習(xí)jQuery插件中的jquery.validate.js表單驗(yàn)證插件使用方法,Validation是歷史最悠久的jQuery插件之一,經(jīng)過了全球范圍內(nèi)不同項(xiàng)目的驗(yàn)證,并得到了許多Web開發(fā)者的好評,感興趣的小伙伴們可以參考一下2015-12-12JQuery+Ajax實(shí)現(xiàn)數(shù)據(jù)查詢、排序和分頁功能
這篇文章介紹了利用JQuery方便實(shí)現(xiàn)基于Ajax的數(shù)據(jù)查詢、排序和分頁功能,需要的朋友可以參考下2015-09-09jQuery實(shí)現(xiàn)動(dòng)態(tài)向上滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)動(dòng)態(tài)向上滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12jquery中$.fn和圖片滾動(dòng)效果實(shí)現(xiàn)的必備知識(shí)總結(jié)
圖片滾動(dòng)效果相信大家都使用過,看上去很簡單的一個(gè)效果,如果想熟練的掌握必須知道jquery、IIFE、setInterval等基礎(chǔ)以及$.fn用法,下面這篇文章主要介紹了關(guān)于jquery中$.fn和圖片滾動(dòng)效果制作的必備知識(shí),需要的朋友可以參考下。2017-04-04