小編給大家分享一下Spring Boot+AngularJS+BootStrap如何實現進度條,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

右玉網站建設公司創新互聯建站,右玉網站設計制作,有大型網站制作公司豐富經驗。已為右玉1000+提供企業網站建設服務。企業網站搭建\成都外貿網站制作要多少錢,請找那個售后服務好的右玉做網站的公司定做!
Spring Boot+AngularJS+BootStrap實現進度條
原理
進度條的原理是在上傳文件的時候,當程序運行到某一個部分,往Session中設置一個1到100的值。然后前臺再每隔很小的一段時間去請求這個值。
在AngularJS中,$http對象有3種狀態,分別是success,progress,error,其中progress方法就會在success方法調用之前(也就是上傳完成之前),不斷地調用。而我們要做的就是在progress中在添加一個請求,去后臺拿我們設置在session中的值。
代碼,這里我用了一個插件用來上傳文件,叫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()">確認</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 == "導入數據不符合格式要求!")
$scope.type = "progress-bar progress-bar-danger progress-bar-striped";//設置進度條樣式
else {
$scope.type = "progress-bar progress-bar-success progress-bar-striped";
$scope.progress = 100;//上傳成功之后,將進度條設置為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;//綁定進度條的值
})
});上傳部分代碼(只需要關注設置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);//解析成功后我將進度設置為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());
//我將處理文件主體進度總量設置為90(5是加上解析部分的進度)
}
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);//保存之后將進度設置為100
res.put("msg", "數據導入成功!");
return res;
}進度條部分代碼,很簡單,就是讀Session中progress的值
public int getProgress(HttpServletRequest request){
return (int) request.getSession().getAttribute("progress");
}看完了這篇文章,相信你對“Spring Boot+AngularJS+BootStrap如何實現進度條”有了一定的了解,如果想了解更多相關知識,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
文章標題:SpringBoot+AngularJS+BootStrap如何實現進度條
文章位置:http://www.yijiale78.com/article34/gipese.html
成都網站建設公司_創新互聯,為您提供App開發、網站設計公司、Google、微信小程序、企業建站、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯