vue2.0 自定义 图片上传(UpLoader)组件
1.自定义组件
UpLoader.vue
<!-- 上传图片 组件 -->
<template>
<div class="vue-uploader">
<!-- 添加图片 及 显示效果 -->
<div class="file-list">
<!-- 图片列表 files -->
<section v-for="(file, index) of files" class="file-item draggable-item">
<img :src="file.src" alt="" ondragstart="return false;">
<p class="file-name">{{file.name}}</p>
<span class="file-remove" @click="remove(index)">+</span>
</section>
<!-- 添加图片按钮 -->
<section v-if="status == 'ready'" class="file-item">
<div @click="add" class="add">
<span>+</span>
</div>
</section>
</div>
<!-- 上传图片操作 及 显示进程 -->
<section v-if="files.length != 0" class="upload-func">
<!-- 上传进度 -->
<div class="progress-bar">
<section v-if="uploading" :width="(percent * 100) + '%'">{{(percent * 100) + '%'}}</section>
</div>
<!-- 操作按钮 -->
<div class="operation-box">
<button v-if="status == 'ready'" @click="submit">上传</button>
<button v-if="status == 'finished'" @click="finished">完成</button>
</div>
</section>
<!-- 调用相机/图库 ref="file" 指定DOM节点 -->
<!-- <input type="file" accept="image/*" @change="fileChanged" ref="file" multiple="multiple"> -->
<input type="file" accept="image/*" @change="fileChanged" ref="file" capture="camera" multiple>
</div>
</template> <script>
export default {
props: {
src: { // 后台接受图片的http地址
type: String,
required: true
}
},
data() {
return {
status: 'ready', // 状态
files: [], // 图片数组
uploading: false, // 进度条
percent: 0, // 上传进度
}
},
methods: {
// 添加图片操作
add() {
this.$refs.file.click();
},
// 上传图片操作
submit() {
if (this.files.length === 0) {
console.warn('no file!');
return
}
// 创建formData对象
const formData = new FormData();
this.files.forEach((item) => {
formData.append(item.name, item.file)
})
const xhr = new XMLHttpRequest()
xhr.upload.addEventListener('progress', this.uploadProgress, false)
xhr.open('POST', this.src, true)
this.uploading = true
xhr.send(formData)
xhr.onload = () => {
this.uploading = false
if (xhr.status === 200 || xhr.status === 304) {
this.status = 'finished'
console.log('upload success!')
} else {
console.log(`error:error code ${xhr.status}`)
}
}
},
// 完成操作 还原状态
finished() {
this.files = []
this.status = 'ready'
},
// 上传图片列表中的某个图片
remove(index) {
this.files.splice(index, 1)
},
// 唤醒相机/图库
fileChanged() {
const list = this.$refs.file.files
for (let i = 0; i < list.length; i++) {
if (!this.isContain(list[i])) {
const item = {
name: list[i].name,
size: list[i].size,
file: list[i]
}
// 转换图片格式
this.html5Reader(list[i], item)
this.files.push(item)
}
}
this.$refs.file.value = ''
},
// 将图片文件转成BASE64格式
html5Reader(file, item){
const reader = new FileReader()
reader.onload = (e) => {
this.$set(item, 'src', e.target.result)
}
reader.readAsDataURL(file)
},
// 判断是否包含
isContain(file) {
this.files.forEach((item) => {
if(item.name === file.name && item.size === file.size) {
return true
}
})
return false
},
// 上传进度
uploadProgress(evt) {
const component = this
if (evt.lengthComputable) {
const percentComplete = Math.round((evt.loaded * 100) / evt.total)
component.percent = percentComplete / 100
} else {
console.warn('upload progress unable to compute')
}
}
}
}
</script> <style lang="less" scoped>
.vue-uploader {
border: 1px solid #e5e5e5;
.file-list {
padding: 10px 0px;
&:after {
content: '';
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
font-size: 0;
}
.file-item {
float: left;
position: relative;
width: 100px;
text-align: center;
img{
width: 80px;
height: 80px;
border: 1px solid #ececec;
}
.file-remove {
position: absolute;
right: 12px;
display: none;
top: 4px;
width: 14px;
height: 14px;
color: white;
cursor: pointer;
line-height: 12px;
border-radius: 100%;
transform: rotate(45deg);
background: rgba(0, 0, 0, 0.5);
}
&:hover{
.file-remove {
display: inline;
}
}
.file-name {
margin: 0;
height: 40px;
word-break: break-all;
font-size: 14px;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
}
}
.add {
width: 80px;
height: 80px;
margin-left: 10px;
float: left;
text-align: center;
line-height: 80px;
border: 1px dashed #ececec;
font-size: 30px;
cursor: pointer;
}
.upload-func {
display: flex;
padding: 10px;
margin: 0px;
background: #f8f8f8;
border-top: 1px solid #ececec;
.progress-bar {
flex-grow: 1;
section {
margin-top: 5px;
background: #00b4aa;
border-radius: 3px;
text-align: center;
color: #fff;
font-size: 12px;
transition: all .5s ease;
}
}
.operation-box {
flex-grow: 0;
padding-left: 10px;
button {
padding: 4px 12px;
color: #fff;
background: #007ACC;
border: none;
border-radius: 2px;
cursor: pointer;
}
}
}
& > input[type="file"] {
display: none;
}
}
</style>
2.页面调用
UpLoadImg.vue
<!-- 上传图片 -->
<template>
<div>
<!-- 标题栏 -->
<mt-header title="上传图片">
<router-link to="/" slot="left">
<mt-button icon="back">返回</mt-button>
</router-link>
</mt-header>
<!-- 内容 -->
<div style="width: 100%;">
<m-up-loader :src="src"></m-up-loader>
</div>
</div>
</template> <script>
// 上传图片组件
import mUpLoader from '../components/UpLoader' export default {
name: 'UploadImg',
components: {
mUpLoader
},
data () {
return {
src: '/api/imgs', // 后台接受图片的路径
}
},
mounted () {
//
},
methods: {
//
}
}
</script> <style lang="less" scoped>
//
</style>
3.效果图

.
vue2.0 自定义 图片上传(UpLoader)组件的更多相关文章
- CKEditor5 + vue2.0 自定义图片上传、highlight、字体等用法
因业务需求,要在 vue2.0 的项目里使用富文本编辑器,经过调研多个编辑器,CKEditor5 支持 vue,遂采用.因 CKEditor5 文档比较少,此处记录下引用和一些基本用法. CKEdit ...
- ueditor 百度编辑器 自定义图片上传路径和格式化上传文件名
今天项目中需要自定义图片上传的保存路径,并且不需要按照日期自动创建存储文件夹和文件名,我的ueditor版本是1.3.6.下面记录一下我配置成功的方法,如果有什么不对的地方欢迎指出,共同学习: 1:我 ...
- vue项目富文本编辑器vue-quill-editor之自定义图片上传
使用富文本编辑器的第一步肯定是先安装依赖 npm i vue-quill-editor 1.如果按照官网富文本编辑器中的图片上传是将图片转为base64格式的,如果需要上传图片到自己的服务器,需要修改 ...
- 2.0 vue2+tinymce实现图片上传与回显
1.效果 2.配置 2.1 在init中添加图片上传函数 // 图片上传 images_upload_handler: (blobInfo, success, failure) => { // ...
- jeecg uedit 自定义图片上传路径
jeecg uedit 图片上传配置自定义物理路径,简单描述:我们知道 jeecg 中使用的 uedit 默认图片上传路径为 "当前项目\plug-in\ueditor\jsp\upload ...
- rails使用bootstrap3-wysiwyg可视化编辑器并实现自定义图片上传插入功能
之前在rails开发中使用了ckeditor作为可视化编辑器,不过感觉ckeditor过于庞大,有很多不需要的功能,而且图片上传功能不好控制不同用户可以互相删除图片,感觉很不好.于是考虑更改可视化编辑 ...
- 使用百度UMeditor富文本编辑器,修改自定义图片上传,修改源码
富文本编辑器,不多说了,这个大家应该都用到过,至于用到的什么版本,那就分很多种 CKEditor:很早以前叫FCK,那个时候也用过,现在改名了,比较流行的一个插件,国外很多公司在用 UEDITOR:百 ...
- Jquery自定义图片上传插件
1 概述 编写后台网站程序大多数用到文件上传,可是传统的文件上传控件不是外观不够优雅,就是性能不太好看,翻阅众多文件上传控件的文章,发现可以这样去定义一个文件上传控件,实现的文件上传的效果图如下: 2 ...
- [Asp.net core 2.0]Ueditor 图片上传
摘要 在项目中要用到富文本编辑器,包含上传图片,插入视频等功能.但ueditor只有.net版本,没有支持core.那么上传等接口就需要自己实现了. 一个例子 首先去百度ueditor官网下载简化版的 ...
随机推荐
- Learning Deconvolution Network for Semantic Segme小结
题目:Learning Deconvolution Network for Semantic Segmentation 作者:Hyeonwoo Noh, Seunghoon Hong, Bohyung ...
- C#-SqlServer连接
C#连接数据库在类方面没有java通用,不同数据库有不同的类库.在这里只做了SqlServer的连接类. public class DbLink { private string config = C ...
- Ubuntu搭建Http服务器用于下载Ubuntu文件
首先安装Apache $ sudo apt-get install apache2 Apache2的默认访问端口为80,当端口被占用时需要更改其访问端口 进入apache2的安装目录 /etc/ap ...
- 建立RSA协商加密的安全信道
在基于TCP长连接的CS链路中,如何保证数据流的安全性是开发者最关注的问题之一.本文深入浅出的给大家介绍一下在TCP连接中,使用RSA协商加密的方式,建立一个安全加密的通信链路,保证数据传输的安全性. ...
- 【Luogu】P2657windy数(数位DP)
题目链接 正式迈入了数位DP的大门…… 心情激动 (看我立个flag,我如果专攻数位DP的话,到wc之前就会有秒数位DP蓝题的能力) 数位DP讲解链接 讲的非常详细,良心博客.比我写的博客加在一起还要 ...
- BZOJ2298 [HAOI2011]problem a 【dp】
题目 一次考试共有n个人参加,第i个人说:"有ai个人分数比我高,bi个人分数比我低."问最少有几个人没有说真话(可能有相同的分数) 输入格式 第一行一个整数n,接下来n行每行两个 ...
- SJTU Summer Camp
Day -2,-1 提前坐飞机来到了上海,在旁边的酒店住下来,晚上去了外滩,在黄浦江边吹着晚风,依旧感慨万千,在衡中高三的一年竟然已经过去,经常出现在噩梦中的高考也已成为历史,然而命运可能并未就此改变 ...
- 刷题总结——树的同构(bzoj4337 树上hash)
Description 树是一种很常见的数据结构. 我们把N个点,N-1条边的连通无向图称为树. 若将某个点作为根,从根开始遍历,则其它的点都有一个前驱,这个树就成为有根树. 对于两个树T1和T2,如 ...
- 浅谈Android反调试 之 PTRACE_TRACEME
反调试原理: 关于Ptrace: http://www.cnblogs.com/tangr206/articles/3094358.html ptrace函数 原型为: #include & ...
- java面试题之happens before原则
JSR-133使用happens-before的概念来指定两个操作之间的执行顺序.由于这两个操作可以在一个线程内,也可以在不同线程之间.因此,JMM可以通过happens-before关系向程序员提供 ...