文件上传下载显示进度(vue)
编写了一个vue组件,可以实时显示文件上传和下载时候的进度
<template>
<div v-show="circleProgress_wrapper_panel_statue" class="circleProgress_wrapper_panel">
<div class="circleProgress_wrapper">
<div class="wrapper right">
<div v-bind:style="{transform:'rotate('+leftDeg + 'deg)'}" class="circleProgress rightcircle"></div>
</div>
<div class="wrapper left">
<div v-bind:style="{transform:'rotate('+rightDeg + 'deg)'}" class="circleProgress leftcircle"></div>
</div>
</div>
<div class="backRect">
<img src="../img/back.png" />
</div>
</div>
</template>
<script>
export default {
data:function(){
return {
circleProgress_wrapper_panel_statue:false,
leftDeg:0,
rightDeg:0
}
},
methods:{
//上传文件
loadFile:function (url, fileList,parm, suc) {
let len = fileList.length,
_this = this;
if(len == 0){
return;
}
this.showCircleProgressWrapperPanel();
let formData = new FormData();
for(let i = 0; i < len; i++){
formData.append("files"+i, fileList[i]);
}
for(let key in parm){
formData.append(key, parm[key]);
}
let xhr = new XMLHttpRequest();
xhr.timeout = 15000;
//xhr.responseType = "json";
xhr.open('POST', url, true);
xhr.onload = function (e) {
if (this.status == 200 || this.status == 304) {
suc(this.responseText);
}
};
xhr.ontimeout = function (e) { alert("超时") };
xhr.onerror = function (e) { alert("error:"+e) };
xhr.upload.onprogress = function (e) {
_this.updateCircleProgressWrapper(e.loaded, e.total);
};
xhr.send(formData);
},
//显示文件
show:function (url, suc) {
let _this = this;
this.showCircleProgressWrapperPanel();
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = "blob";
xhr.onprogress = function (e) {
_this.updateCircleProgressWrapper(e.loaded, e.total);
};
xhr.onload = function () {
if (this.status === 200) {
let blob = this.response;
suc(blob);
}
}
xhr.send();
},
test:function(){
this.showCircleProgressWrapperPanel();
let _this = this,
i = 0,
total = 10;
window.interval = window.setInterval(function () {
_this.updateCircleProgressWrapper(i, total);
i = i + 1;
if(i > total){
window.clearInterval(window.interval);
}
}, 100);
},
//根据上传字节数计算角度
updateCircleProgressWrapper:function (loaded, total) {
let deg = 360 * loaded / total;
this.updateCircleProgressRotate(deg);
},
//隐藏滚动条
hideCircleProgressWrapperPanel:function () {
this.circleProgress_wrapper_panel_statue = false;
this.setCircleProgressRotate(45, 45);
},
//显示滚动条
showCircleProgressWrapperPanel:function () {
this.circleProgress_wrapper_panel_statue = true;
},
//更新滚动条角度
updateCircleProgressRotate:function (deg) {
let defaultDeg = deg;
if (deg <= 180) {
deg = deg + 45;
this.setCircleProgressRotate(deg, 45);
} else {
(deg > 360) ? deg = 360 : "";
deg = deg - 180;
deg = deg + 45;
this.setCircleProgressRotate(225, deg);
}
if (defaultDeg == 360) {
this.hideCircleProgressWrapperPanel();
}
},
//设置滚动条角度
setCircleProgressRotate:function(leftDeg, rightDeg) {
this.leftDeg = leftDeg;
this.rightDeg = rightDeg;
}
},
mounted:function(){
}
}
</script>
<style scoped>
.circleProgress_wrapper_panel
{
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
background-color:rgba(0,0,0,0.8);
}
.circleProgress_wrapper
{
position:absolute;
top:50%;
left:50%;
width: 100px;
height: 100px;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
.wrapper{
width: 50px;
height: 100px;
position: absolute;
top:0;
overflow: hidden;
}
.right{
right:0;
}
.left{
left:0;
}
.circleProgress{
width: 80px;
height: 80px;
border:10px solid transparent;
border-radius: 50%;
position: absolute;
top:0;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.rightcircle{
border-top:10px solid #767676;
border-right:10px solid #767676;
right:0;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.leftcircle{
border-bottom:10px solid #767676;
border-left:10px solid #767676;
left:0;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
.backRect
{
position:absolute;
top:50%;
left:50%;
width:79px;
height:79px;
border:10px solid #e5e5e5;
background-color:White;
border-radius: 50%;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
z-index:-1;
}
.backRect img
{
width:100%;
position:absolute;
top:50%;
left:50%;
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
</style>
文件上传使用方法:
引入组件:import fileLoading from '../component/fileLoading.vue'
注册组件:
components: {
fileLoading:fileLoading
}
声明组件:<fileLoading ref="fileLoadingPonent"></fileLoading>
使用:
let fileData = [],
_this = this,
imgList = this.imgList,
len = this.imgList.length,
item = null;
for(let i = 0; i < len; i++){
item = imgList[i];
fileData.push(item.data);//将多个文件对应的file对象放入数组
}
this.$refs.fileLoadingPonent.loadFile(Config.report,fileData,{content:this.reportContent},function(data){
let result = JSON.parse(data);
if(result.res == "1"){
_this.$store.commit('report/setReportContent',{value:""})
_this.$store.commit('report/clearImgList',{})
_this.clearIcon();
}else{
alert(result.msg);
}
});
下载文件使用:
使用场景:图片很大的时候,先加载缩略图,然后点击缩略图再加载大图
引入组件:import fileLoading from '../component/fileLoading.vue'
注册组件:
components: {
fileLoading:fileLoading
}
声明组件:<fileLoading ref="fileLoadingPonent"></fileLoading>
使用:
loadBigImg:function(src,index,event){
let _this = this;
let NSrc = src.replace("_s","");//缩略图和大图的区别是缩略图在大图的名字后面多了_s,如:大图:name.jpg,对应的缩略图:name_s.jpg
let NPath = Config.hostImg + NSrc;//大图的显示地址
let target = event.currentTarget;
this.$refs.fileLoadingPonent.show(NPath,function(blob){
let imgEle = document.createElement("img");//创建新图片控件
let parentNode = target.parentNode;
parentNode.removeChild(parentNode.getElementsByTagName("img")[0]);//删除之前的缩略图
imgEle.style.width = "100%";
imgEle.onload = function (e) {
window.URL.revokeObjectURL(imgEle.src); //释放。
};
imgEle.src = window.URL.createObjectURL(blob); //有问题,将blob加载到img中 由于blob太大 会有性能影响 应该怎么在加载之后 如何释放呢:
parentNode.appendChild(imgEle);
//_this.$store.commit('details/changeImgList',{value:NSrc,index:index});
target.style.display = "none";//隐藏提示点击加载文字
});
}
文件上传下载显示进度(vue)的更多相关文章
- Retrofit2文件上传下载及其进度显示
序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...
- Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)
Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
- 用JAVA实现大文件上传及显示进度信息
一. 大文件上传基础描述: 各种WEB框架中,对于浏览器上传文件的请求,都有自己的处理对象负责对Http MultiPart协议内容进行解析,并供开发人员调用请求的表单内容. 比如: Spring 框 ...
- Asp.Net 无刷新文件上传并显示进度条的实现方法及思路
相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来 实现呢?我并不否认”拿来主义“,只是我个人更喜欢凡是求个所以 ...
- 仵航说 前后端分离,文件上传下载(springBoot+vue+elementUI)仵老大
1.介绍 本文主要是介绍前后端分离的上传下载,后端使用的是SpringBoot,持久层用的是mybatis-plus,前端用的Vue,UI用的elementUI,测试了一下,文本,图片,excel ...
- 使用Typescript重构axios(二十五)——文件上传下载进度监控
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- SpringMVC文件上传下载
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...
- JavaWeb实现文件上传下载功能实例解析
转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...
随机推荐
- SetupFactory 制作安装包
SetupFactory9.0.3.0Trial汉化破解版+使用教程 https://download.csdn.net/download/u010188178/10652645
- 作业二 分布式版本控制系统Git的安装与使用
第一步:Git bash配置 修改用户名和邮箱地址: $ git config --global user.name "zzj" $ git config --global use ...
- linux学习:【第3篇】远程连接及软件安装
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! linux学习:[第3篇]远程连接及软件安装 远程连接 xshell , xftp软件官网 : ...
- threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: com/dyuproject/protostuff/MapSchema$MessageFactory] with root cause
错误记录 前几天朋友问我一个错误,顺便记录一下,关于redis 工具类,protostuff序列化报错. threw exception [Handler processing failed; nes ...
- vue router相关用法
router.push(location) 想要导航到不同的 URL,则使用 router.push 方法.这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之 ...
- 解决win10无法访问共享
一台win10共享的文件夹,有的电脑是可以访问的,我的win10 访问不了,说什么 遇到未知错误,用以下方法得以解决 ----------------------------------------- ...
- [js]this关键字代表当前执行的主体
点前是谁,this就是谁 <div id="div1" class="div1"></div> <div id="div ...
- 2018年工作终总结&规划
收获满满的2018 收获总结: 1. 换了家有地区牌照的公司,薪酬涨了那么一点点,但是工作压力.强度下降不少,这样有更多时间来学习新知识. 2. 跟同事一起接了维护后台管理系统的私活,每个月多了一点点 ...
- Oracle 24角色管理
了解什么是角色 Oracle角色(role)就是一组权限(或者说是权限的集合). 用户可以给角色赋予指定的权限,然后将角色赋给相应的用户. 三种标准的角色 connect(连接角色) 拥有connec ...
- 关于view.py 中 ajax json 的用法
1. data=models.Citys.objects.filter(upid=0) data 的数据形式是一个查询集(也是一个列表,查询出来的每一条数据是一个对象): <QuerySet [ ...