我后端的,刚接触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. PostgreSQL SQL优化之NOT IN问题

    在我们平时写SQL时,如果遇到需要排除某些数据时,往往使用id <> xxx and id <> xxx,进而改进为id not in (xxx, xxx); 这样写没有问题, ...

  2. 在Ubuntu上安装Hive

    1.下载hive2.3.5 解压缩并改名为hive目录,放到/usr/local下(注意权限) sudo mv apache-hive-2.3.5-bin /usr/local/hive 2.修改目录 ...

  3. Spring使用注解实现AOP

    一.AspectJ概述 AspectJ是一个面向切面的框架,它扩展了Java语言.定义了AOP语法,能够在编译期提供代码的织入,它提供了一个专门的编译期用来生成遵守字节编码规范的Class文件. @A ...

  4. codeforces 620C

    题目链接:https://codeforces.com/problemset/problem/620/C 题目分析 题意:给你一串珍珠,每个珍珠都有一个对应值,需要分割这n个珍珠(必须连续),使得每一 ...

  5. 2019CCPC-江西省赛 -A Cotree (树形DP,求树上一点到其他点的距离之和)

    我是傻逼我是傻逼 #include<bits/stdc++.h> using namespace std; const int maxn=4e5+50; typedef long long ...

  6. FFmpeg4.0笔记:本地媒体文件解码、帧格式转换、重采样、编码、封装、转封装、avio、硬解码等例子

    Github https://github.com/gongluck/FFmpeg4.0-study/blob/master/official%20example/my_example.cpp #in ...

  7. springboot(二十)-配置文件 bootstrap和application区别

    用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...

  8. UI测试

    先是从一张图开始,让大家看看这个图里有什么不妥: 接着告诉大家具体有哪些不妥: 然后结合这个找茬的过程分享下界面测试的概念和方法. 界面测试:简称UI测试,测试功能模块界面上看到的所有元素(包括空文字 ...

  9. 网速监控-nload

    用来监控系统网卡实时网速的. 安装 yum install nload -y # 或 apt install nload -y 使用 # 直接运行默认监控第一个网卡, 使用上下方向键来切换网卡. nl ...

  10. Nginx之常用操作

    1) 将XXX.com 重定向到 www.XXX.com server { client_max_body_size 20m; listen ; server_name www.xxx.com xxx ...