element中upload单图片转base64后添加进数组,请求接口
//先上代码
<template>
<!-- data绑定的参数
getuploadloge: [
{
url: '',
name: ''
}
],
-->
<!-- 编辑操作模板
this.uploadstatue = false
//在父类给该upload组件添加v-if事件
this.$nextTick(() => {
this.uploadstatue = true
})
this.form = res.data
//给upload传参
this.getuploadloge = [
{
name: res.data.Name,
url: res.data.Logo,
status: 0
}
] -->
<!-- 新增模板
this.uploadstatue = false
//在父类给该upload组件添加v-if事件
this.$nextTick(() => {
this.uploadstatue = true
})
//给upload传参
this.getuploadloge[0].name = ''
this.getuploadloge[0].url = ''
this.getuploadloge[0].status = 1
-->
<!--上边的添加事件是在父类引用改子类的标签里加v-if事件,来保证组件从新加载,否则组件不会刷新-->
<div>
<p >
只能上传jpg/png文件,且不超过1024kb
</p>
<el-upload
action="#"
:auto-upload="false"
:file-list="fileList"
:multiple = "false"
:limit="1"
accept=".jpg,.jpeg,.png"
list-type="picture-card"
:class="{hide:hideUpload}"
:on-change="maximg"
:before-upload="beforeAvatarUpload"
:on-remove="handleRemove">
<!-- <img :src="backimg"> -->
<i class="el-icon-plus"></i>
</el-upload>
</div> </template> <script>
export default {
props: ['getuploadloge'],
data() {
return {
hideUpload: false, //隐藏添加窗口
dialogVisible: false, //照片模态框
fileList: [], //图片数组
uploaddate: [], //子组件数组
backimg: '../../../static/images/bank_upload_logo.png'
}
},
mounted() {
//获取图片数据
this.getupload()
},
methods: {
//获取文件数据
getupload() {
this.uploaddate = Object.assign(this.getuploadloge)
if (this.uploaddate[0].status === 1) {
this.fileList.length = 0
this.handleRemove('', this.fileList)
} else {
if (this.uploaddate[0].name) {
this.fileList = this.uploaddate
this.hideUpload = this.fileList.length >= 1
}
}
},
//文件列表移除文件时的钩子
handleRemove(file, fileList) {
if ((fileList.length = 0)) {
this.hideUpload = true
} else {
this.hideUpload = fileList.length >= 1
this.$emit('uploaddate', '')
}
},
handlePictureCardPreview(file) {},
//文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用
maximg(file, fileList) {
const isIMAGE = file.name.substring(file.name.lastIndexOf('.') + 1)
const isLt1M = file.size / 1024 / 1024
if (isIMAGE !== 'jpg' && isIMAGE !== 'png' && isIMAGE !== 'jpeg') {
this.$message.error('上传文件只能是jpg和png格式!')
this.fileList.length = 0
this.handleRemove(file, fileList)
}
if (isLt1M > 1) {
this.$message.error('上传文件大小不能超过 1MB!')
this.fileList.length = 0
this.handleRemove(file, fileList)
}
this.hideUpload = fileList.length >= 1
let reader = new FileReader() //html5读文件
//转BASE64
reader.readAsDataURL(file.raw)
reader.onload = e => {
this.uploaddate = e.target.result
//读取完毕后调用接口
this.$emit('uploaddate', this.uploaddate)
} return isIMAGE && isLt1M
},
beforeAvatarUpload(file, fileList) {
// console.log('123465789')
}
},
name: 'Upload'
}
</script>
<style>
.hide .el-upload--picture-card {
display: none;
}
</style>
<style scoped>
</style>
//因为我的dialog写的公用组件,需要的可以看下我的其他文章.
//因为我的新增与修改是一个模态框,所以判断那一块可以自行修改.
//file-list绑定的数组格式是:File-list:[{name:名字,url:地址}]
//只要符合这个数组就可以回显.
//其中的属性代表什么官网里有详细解释,在这里就不一一解释了.
//说几个特殊点的.accept属性是点击上传按钮后,弹出的图片选择框的默认后缀.目的是为了加载迅速,如果不加此属性,点击后页面文件过多的情况下会加载很慢.
//注意:accept后面跟的不是验证,accept后面跟的不是验证,accept后面跟的不是验证,如果需要验证请在方法中写校验规则
element中upload单图片转base64后添加进数组,请求接口的更多相关文章
- Angular中$watch实现控件改变后实时发送HTTP请求
实现代码如下 <!DOCTYPE html> <html ng-app="myServiceApp"> <head> <meta char ...
- java消除整型数组中重复的元素,排序后输出新数组
法一: import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(S ...
- 分离与继承的思想实现图片上传后的预览功能:ImageUploadView
本文要介绍的是网页中常见的图片上传后直接在页面生成小图预览的实现思路,考虑到该功能有一定的适用性,于是把相关的逻辑封装成了一个ImageUploadView组件,实际使用效果可查看下一段的git效果图 ...
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
Visual Studio 2017中使用正则修改部分内容 最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效 ...
- Vue项目打包发布后CSS中的背景图片不显示
相信有很多同学在学习vue的刚开始都遇到过项目打包发布后发现CSS中的背景图片不显示,具体如何解决只需要更改bind的配置即可 修改 build/utils.js 中的 generateLoaders ...
- 【PHP】图片转换为base64,经过post传输后‘+’会变成 ‘空格’
图片转换为base64,经过post传输后‘+’会变成 ‘空格’, 需要用PHP 处理一下 $str= $_POST['img_data']; $str= str_replace(' ','+',$s ...
- 在vue项目中,解决如何在element表格中循环出图片列!
效果图: 1,vue项目环境 2,引入element-ui组件 3,制作表格 此处省去制作循环表格数据那步,想看的可以找回我的博客:element中的表格处理:循环出表格数据 今天想在表格出循环出一列 ...
- C#中图片转换为Base64编码,Base64编码转换为图片
#region 图片转为base64编码的字符串 public string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = ...
- JS上传图片-通过FileReader获取图片的base64
下面文章,我想要的是: FileReader这个对象,可以借助FileReader来获取上传图片的base64,就可以在客户端显示该图片了.同时,还可以把该图片的base64发送到服务端,保存起来. ...
随机推荐
- [Bzoj1022][SHOI2008]小约翰的游戏John(博弈论)
1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2976 Solved: 1894[Submit] ...
- MAC地址泛红攻击
一.环境 IP地址: Windows10 IP:10.13.153.55 Windows7: IP:192.168.83.130 Linux: IP:192.168.83.129 ...
- Eclipse 中 新建maven项目 无法添加src/main/java 问题
eclipse创建maven web项目,在选择maven_archetype_web原型后,默认只有src/main/resources这个Source Floder. 按照maven目录结构,添加 ...
- HDU 4786 Fibonacci Tree(生成树,YY乱搞)
http://acm.hdu.edu.cn/showproblem.php? pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others ...
- VS2012关于hash_map的使用简略
VS关于hash_map使用的一些经常使用构造方法汇总,包含基本类型和结构体,相信够一般模仿使用: # include<hash_map> #include<iostream> ...
- Xpath—解决这个问题的良药
何为良药? 因为在XML中存在一些问题和缺陷,针对这些问题就产生了响应的解决方式.如: getElementById方法在解析XML时因为一些原因适不适合的: 首先XML中每一个元素节点不一定有id属 ...
- SDUT 3503 有两个正整数,求N!的K进制的位数
有两个正整数,求N!的K进制的位数 题目链接:action=showproblem&problemid=3503">http://sdutacm.org/sdutoj/prob ...
- Thinking in Java -- 类型信息RTTI
Thinking in Java – 类型信息 个人感觉 java 中的比較难的部分了,在看了些netty源代码发现事实上这块很实用. 这章重点是RTTI和反射.先说下自己的理解 RTTI是执行时识别 ...
- php截取某二个特殊字符串间的某段字符串
在php开发的过程中,有时候会用到截取某二个特殊字符串间的某个字符串,并对这个字符串做特殊的处理,那么对截取出来的字符串做什么特殊处理我们临时无论.我们今天先讲php截取某二个特殊字符串间的某个字符串 ...
- ubuntu查看文件的权限
查看linux文件的权限: 查看path路径下名为filename的文件或文件夹的权限: ls -l path/filename ls -l path/filename 查看path路径下的所有文件的 ...