小編給大家分享一下Spring Boot+AngularJS+BootStrap如何實(shí)現(xiàn)進(jìn)度條,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
右玉網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,右玉網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為右玉1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個(gè)售后服務(wù)好的右玉做網(wǎng)站的公司定做!
Spring Boot+AngularJS+BootStrap實(shí)現(xiàn)進(jìn)度條
原理
進(jìn)度條的原理是在上傳文件的時(shí)候,當(dāng)程序運(yùn)行到某一個(gè)部分,往Session中設(shè)置一個(gè)1到100的值。然后前臺(tái)再每隔很小的一段時(shí)間去請求這個(gè)值。
在AngularJS中,$http對(duì)象有3種狀態(tài),分別是success,progress,error,其中progress方法就會(huì)在success方法調(diào)用之前(也就是上傳完成之前),不斷地調(diào)用。而我們要做的就是在progress中在添加一個(gè)請求,去后臺(tái)拿我們設(shè)置在session中的值。
代碼,這里我用了一個(gè)插件用來上傳文件,叫ng-file-upload
html
<input type="file" data-ng-model="file"> <uib-progress data-ng-show="progress"> <uib-bar value="progress" type="{{type}}" data-ng-bind="progress + '%'"/> </uib-progress> <span class="err" data-ng-show="isShowMsg" data-ng-bind="Msg"></span> <button class="btn btn-primary" type="button" data-ng-click="upload()">確認(rèn)</button>
js
Upload.upload( { url: "", data: { file: file }, method: 'post' }).then(function (res) { //這里是success方法 $scope.isShowMsg = true; $scope.Msg = res.data.msg; if($scope.Msg == "導(dǎo)入數(shù)據(jù)不符合格式要求!") $scope.type = "progress-bar progress-bar-danger progress-bar-striped";//設(shè)置進(jìn)度條樣式 else { $scope.type = "progress-bar progress-bar-success progress-bar-striped"; $scope.progress = 100;//上傳成功之后,將進(jìn)度條設(shè)置為100 } }, function (){ //這里是error方法 }, function (){ //這里是progress方法 $scope.type = "progress-bar progress-bar-info progress-bar-striped"; $http({ url:"", method: "get" }).success(function (res) { $scope.progress = res;//綁定進(jìn)度條的值 }) });
上傳部分代碼(只需要關(guān)注設(shè)置session的地方
public Map<String, Object> batchModify(InputStream inputStream,HttpSession session) { Map<String, Object> res = new HashMap<>(); Workbook workbook = null; try { workbook = Util.createWorkbook(inputStream); } catch (InvalidFormatException | IOException e) { e.printStackTrace(); } session.setAttribute("progress", 5);//解析成功后我將進(jìn)度設(shè)置為5 Sheet sheet = workbook.getSheetAt(0); Map<String, Object> roleWithPages = new HashMap<>(); for (int i = 1; i <= sheet.getLastRowNum(); i++) { Row r = sheet.getRow(i); if (r == null || r.getCell(0) == null || r.getCell(1) == null) continue; Set<Page> pages = null; if (roleWithPages.get(r.getCell(0).toString()) == null) { pages = new HashSet<>(); } else { pages = (Set<Page>) roleWithPages.get(r.getCell(0).toString()); } Page p = new Page(); p.setId(Math.round(r.getCell(1).getNumericCellValue())); pages.add(p); roleWithPages.put(r.getCell(0).toString(), pages); session.setAttribute("progress", 5 + i*90/sheet.getLastRowNum()); //我將處理文件主體進(jìn)度總量設(shè)置為90(5是加上解析部分的進(jìn)度) } List<Role> roles = new ArrayList<>(); for (String rolename : roleWithPages.keySet()) { Role role = repo.findByName(rolename); role.setPages((Set<Page>) roleWithPages.get(rolename)); roles.add(role); } repo.save(roles); session.setAttribute("progress", 100);//保存之后將進(jìn)度設(shè)置為100 res.put("msg", "數(shù)據(jù)導(dǎo)入成功!"); return res; }
進(jìn)度條部分代碼,很簡單,就是讀Session中progress的值
public int getProgress(HttpServletRequest request){ return (int) request.getSession().getAttribute("progress"); }
看完了這篇文章,相信你對(duì)“Spring Boot+AngularJS+BootStrap如何實(shí)現(xiàn)進(jìn)度條”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章標(biāo)題:SpringBoot+AngularJS+BootStrap如何實(shí)現(xiàn)進(jìn)度條
文章位置:http://bm7419.com/article34/gipese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站設(shè)計(jì)公司、Google、微信小程序、企業(yè)建站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)