视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
vue-cli3.0+element-ui上传组件el-upload的使用
2020-11-27 22:03:09 责编:小采
文档


这是我设置的一些初始值

下面最重要的就是钩子函数了

1、handleExceed是文件超出个数时的钩子
private handleExceed(files: any, fileList: any) {
 if (fileList.length > 20) {
 this.$message.error('最多允许上传20个文件');
 return false;
 }
 }
2、handleBeforeUpload文件上传前的钩子,可以做一些拦截,return false,则停止上传
private handleBeforeUpload(file: any) {
 // 文件大小
 const isLt5M = file.size / 1024 / 1024 < 5;
 if (!isLt5M) {
 this.$message.error('不得超过5M');
 return isLt5M;
 }
 // 文件类型
 const name = file.name ? file.name : '';
 const ext = name
 ? name.substr(name.lastIndexOf('.') + 1, name.length)
 : true;
 const isExt = this.accept.indexOf(ext) < 0;
 if (isExt) {
 this.$message.error('请上传正确的格式类型');
 return !isExt;
 }
 // 大小和类型验证都通过后,给自定义的列表中添加需要的数据
 this.objAddItem(this.tempArr, file);
 }
3、handleProgress文件上传时的钩子,更新进度条的值
private handleProgress(event: any, file: any, fileList: any) {
 this.tempArr.forEach((element: any, index: number) => {
 if (element.uid === file.uid) {
 // 更新这个uid下的进度
 const progress = Math.floor(event.percent);
 // 防止上传完接口还没有返回成功值,所以此处给定progress的最大值为99,成功的钩子中再置为100
 element.progress = progress === 100 ? 99 : progress;
 this.$set(this.tempArr, index, element);
 this.$emit('changeFileList', this.tempArr);
 }
 });
 }
4、handleSuccess文件上传成功时的钩子
private handleSuccess(response: any, file: any, fileList: any) {
 this.tempArr.forEach((element: any, index: number) => {
 if (element.uid === file.uid) {
 element.progress = 100;
 // element.url为下载地址,一般后端人员会给你返回
 // 我这边为了做后面的下载,先写死链接供测试
 element.url = 'http://originoo-1.b0.upaiyun.com/freepic/32233.jpg!freethumb';
 this.$message.success('文件上传成功');
 this.$set(this.tempArr, index, element);
 this.$emit('changeFileList', this.tempArr);
 }
 });
 // response是后端接口返回的数据,可以根据接口返回的数据做一些操作
 // 示例
 // const bizCode = response.rspResult.bizCode;
 // switch (bizCode) {
 // case 200:
 // this.tempArr.forEach((element: any, index: number) => {
 // if (element.uid === file.uid) {
 // element.progress = 100;
 // element.url = response.data.url; // 这是后端人员给我返回的下载地址
 // this.$message.success('文件上传成功');
 // this.$set(this.tempArr, index, element);
 // this.$emit('changeFileList', this.tempArr);
 // }
 // });
 // break;
 // default:
 // this.tempArr.forEach((element: any, index: number) => {
 // if (element.uid === file.uid) {
 // this.tempArr.splice(index, 1); // 上传失败删除该记录
 // this.$message.error('文件上传失败');
 // this.$emit('changeFileList', this.tempArr);
 // }
 // });
 // break;
 // }
 }
5、handleError文件上传失败时的钩子
private handleError(err: any, file: any, fileList: any) {
 this.tempArr.forEach((element: any, index: number) => {
 if (element.uid === file.uid) {
 this.tempArr.splice(index, 1); // 上传失败删除该记录
 this.$message.error('文件上传失败');
 this.$emit('changeFileList', this.tempArr);
 }
 });
 }
添加数据函数
private objAddItem(tempArr: any[], file: any) {
 const tempObj = {
 uid: file.uid, // uid用于辨别文件
 originalName: file.name, // 列表显示的文件名
 progress: 0, // 进度条
 code: 200, // 上传状态
 };
 tempArr.push(tempObj);
 this.$emit('changeFileList', tempArr);
 }
上传的文件下载封装
private downloadFileFun(url: any) {
 const iframe: any = document.createElement('iframe') as HTMLIFrameElement;
 iframe.style.display = 'none'; // 防止影响页面
 iframe.style.height = 0; // 防止影响页面
 iframe.src = url;
 document.body.appendChild(iframe); // 这一行必须,iframe挂在到dom树上才会发请求
 // 5分钟之后删除(onload方法对于下载链接不起作用,就先抠脚一下吧)
 setTimeout(() => {
 iframe.remove();
 }, 5 * 60 * 1000);
 }

下载本文
显示全文
专题