我后端的,刚接触vue个星期,根据需求写了个上传控件,很灵活的。没有看element el-upload源码,样式用的element的。感觉vue确实好用。

先看样子:

<!-- 单文件上传组件 -->
<template>
<div>
<input type="file" id="file" hidden @change="fileChange" :accept="accept">
<div v-if="upMode=='url'" style="width: 100%;display:inline-flex;">
<el-input :value="path" disabled></el-input>
<el-button size="small" type="primary" @click="btnChange">{{autoUpload?label:'选择文件'}}</el-button>
<el-button v-if="!autoUpload && loadButton" size="small" type="success" @click="uploadClick">{{label}}</el-button>
</div>
<div v-else-if="upMode=='head'" class="upmode-head">
<div class="el-upload el-upload--picture-card" @click="btnChange">
<i v-if="path==''" class="el-icon-plus"></i>
<img v-else :src="path">
</div>
<el-button v-if="!autoUpload && loadButton" size="small" type="success" @click="uploadClick">{{label}}</el-button>
</div>
<div v-else-if="upMode=='variable'" class="el-upload-dragger" @click="btnChange">
<div v-if="path==''">
<i class="el-icon-upload"></i><div class="el-upload__text"><em>{{label}}</em></div>
</div>
<img v-else :src="path">
</div> </div>
</template> <script>
import glob from '@/utils/global.js'
export default {
model: {
prop: 'path',
event: 'onsuccess'
},
props: {
label: {type: String, default: '上传'},//按钮文字
accept: {String,default:'image/jpeg,image/png,image/x-icon'},//预选文件类型
autoUpload:{type:Boolean, default:true},//是否自动上传
loadButton:{type:Boolean, default:true},//手动上传时是否生成上传按钮
path:String,//回显地址
action: {type: String, default: '/api/upload/img'},//传入action可以调用不同接口适应不同文件类型上传
upMode:{type: String, default: 'url'},//上传控件模式url,head,variable
},
methods: {
btnChange() {
var file = document.getElementById('file');
file.click();
},
fileChange(e){
try {
const fu = this.getFile();
if(fu==null)return;
if(!this.beforeUpload(fu))return;
if(this.autoUpload){
this.submit(fu);
}else{
if(this.upMode=='url')return;
//本地预览
var reader = new FileReader();
reader.readAsDataURL(fu);
reader.onload = function(e) {
var src = this.result;
var str = '<img src="'+src+'">';
var target=this.upMode=='head'?'.el-upload--picture-card':'.el-upload-dragger';
var nim = document.querySelector(target);
if(nim) {
nim.innerHTML='';
nim.insertAdjacentHTML("beforeEnd", str)
}
};
} } catch (error) {
console.debug('choice file err:',error);
}
},
beforeUpload(f) {
const maxSzie = glob.Config.UploadSize;
if (f.size/1024/1024 > maxSzie) {
this.$message.error('上传文件大小不能超过'+maxSzie+'M.');
return false;
}
var types = glob.Config.UploadType;
for (let i = 0; i < types.length; i++) {
if(types[i]===f.type) return true;
}
this.$message.error('上传文件格式不允许!');
return false;
},
uploadClick(e){
this.submit();
},
submit(file){
try {
if(!file) file = this.getFile();
if(file==null)return;
var isUpload =true;
this.$emit('onbefore', file);
if(!isUpload)return;
const url =process.env.Store_API + this.action;
var fd = new window.FormData();
// 配置post请求的参数。参数名fileType,值为category(看后端的具体要求)
fd.append('fileType', 'category');
fd.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.send(fd);
xhr.onload = () => {
if (xhr.status === 200) {
var res = JSON.parse(xhr.responseText);
if(res.code==1) {
var imgurl = glob.StaticStoreHost+res.data;
this.$emit('onsuccess', imgurl);
}else{
this.$message(res.message);
}
}
}
} catch (error) {
console.debug('upload file err:',error);
}
},
getFile(){
var file = document.getElementById('file');
if(file.files.length==0){
this.$message('没有选择文件');
return null;
}
return file.files[0];
},
}, }
</script>

其实就这个小组件就体现到封装、继承、多态的思想,多花时间思考,代码就能简洁、灵活、可用性高。

样式也有重写,需要样式的评论支持下,谢谢。

vue-upload 封装组件-上传组件的更多相关文章

  1. 微信小程序简单封装图片上传组件

    微信小程序简单封装图片上传组件 希望自己 "day day up" -----小陶 我从哪里来 在写小程序的时候需要上传图片,个人觉得官方提供的 Uploader 组件不是太好用, ...

  2. 从0开始做一个的Vue图片/ 文件选择(上传)组件[基础向]

    原文:http://blog.csdn.net/sinat_17775997/article/details/58585142 之前用Vue做了一个基础的组件 vue-img-inputer ,下面就 ...

  3. vue里图片压缩上传组件

    //单图上传 <template> <div> <div class="uploader" v-if='!dwimg'> <van-upl ...

  4. React后台管理手动封装图片上传组件

    分为两个文件夹,index.js(逻辑文件) styled.js(样式文件) index.js文件,编写完成之后在对应的地方引入即可 import React from "react&quo ...

  5. VUE -- iview table 组件 中使用 upload组件 上传组件 on render 事件不会触发问题

    碰到的问题是: upload 组件在 on中写的监听事件不会被触发 在 props 中来监听:==>

  6. vue.js框架图片上传组件

    html: <div id="app"> <div class="hello"> <div class="upload& ...

  7. 封装Vue Element的upload上传组件

    本来昨天就想分享封装的这个upload组件,结果刚写了两句话,就被边上的同事给偷窥上了,于是在我全神贯注地写分享的时候他就神不知鬼不觉地突然移动到我身边,腆着脸问我在干啥呢.卧槽你妈,当场就把我吓了一 ...

  8. 封装react antd的upload上传组件

    上传文件也是我们在实际开发中常遇到的功能,比如上传产品图片以供更好地宣传我们的产品,上传excel文档以便于更好地展示更多的产品信息,上传zip文件以便于更好地收集一些资料信息等等.至于为何要把上传组 ...

  9. Vue.js 3.0搭配.NET Core写一个牛B的文件上传组件

    在开发Web应用程序中,文件上传是经常用到的一个功能. 在Jquery时代,做上传功能,一般找jQuery插件就够了,很少有人去探究上传文件插件到底是怎么做的. 简单列一下我们要做的技术点和功能点 使 ...

随机推荐

  1. luoguP3390(矩阵快速幂模板题)

    链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...

  2. 什么是云数据库RDS PPAS 版

    云数据库PPAS版,是阿里云与EnterpriseDB公司合作基于PostgreSQL高度兼容Oracle语法的数据库服务,为用户提供易于操作的迁移工具,兼容范围涵盖:PL/SQL.数据类型.高级函数 ...

  3. 设计模式:策略模式(Stratege)

    首先我们需要知道策略模式与状态模式是如此的相似,就犹如一对双胞胎一样.只不过状态模式是通过改变对象内部的状态来帮助对象控制自己的行为,而策略模式则是围绕可以互换的算法来创建成功业务的.两者都可用于解决 ...

  4. C语言&*符号使用及大端法小端法测试

    工具:Microsoft Visual C++ 6.0 例子: int a = 1; int* b = &a; C语言规定a表示存储单元中的数据,&a表示存储单元的地址,b存储的就是a ...

  5. QQ登錄實現功能

    <html> <head> <title></title> </head> <body> <form> <p& ...

  6. 关于Faster-RCNN训练细节

    Faster RCNN训练: 四部训练法: Faster R-CNN,可以大致分为两个部分,一个是RPN网络,另一个是Fast R-CNN网络,前者是一种候选框(proposal)的推荐算法,而后者则 ...

  7. LinkedList实现基于LRU算法的缓存

    LinkedList实现基于LRU算法的缓存 2015年08月07日 18:18:45 秦江波 阅读数 2068 文章标签: java算法linkedlist缓存LRU更多 分类专栏: Java   ...

  8. Collection接口的子接口——Queue接口

    https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html public interface Queue<E> exten ...

  9. 转:spring中InitailizingBean接口的简单理解

    转自:https://www.cnblogs.com/wxgblogs/p/6849782.html spring中InitializingBean接口使用理解   InitializingBean接 ...

  10. se37 函数中的异常使用

    一种是rase <exceptions> FUNCTION ztest. *"-------------------------------------------------- ...