upload上传组件的使用方法

上传图片后自动上传(也可以手动上传),图片上传成功后由后端返回特定图片地址,在表单提交后将表单数据同图片地址一并返回即可完成图片上传功能。

组件HTML

<!-- 上传图片 -->
<div style="margin: 4px 0">图片上传(仅支持jpg、png格式)</div>
<el-upload
class="upload"
:class="{ hide: hideUpload }"
action="#"
list-type="picture-card"
:auto-upload="true"
:limit="3"
:http-request="uploadFiles"
:before-upload="beforeAvatarUpload"
:on-change="onChange"
:on-success="onSuccess"
:on-remove="handleRemove"
:file-list="fileList"
accept="image/jpeg,image/gif,image/png,image/jpg"
>
<i slot="default" class="el-icon-plus"></i>
</el-upload>

需要声明部分变量

方法

1.将图片转换为base64的方法

//文件转base64
getBase64(file) {
return new Promise((resolve, reject) => {
let reader = new FileReader()
let fileResult = ''
reader.readAsDataURL(file) //开始转
reader.onload = function () {
fileResult = reader.result
} //转 失败
reader.onerror = function (error) {
reject(error)
} //转 结束 咱就 resolve 出去
reader.onloadend = function () {
resolve(fileResult)
}
})
},

2.数组根据内容删除对应元素

// 对应内容的索引
Array.prototype.indexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
// 删除对应索引的内容
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};

3.编辑图片并回显

已经上传过的图片如果需要进行编辑的话需要在进行上传之前先将已经上传过的图片已规定的格式存入图片列表中

放在切换至引用

this.picArr = [];
// 编辑上传过的图片
if (row.picList.length > 0) {
for (let i = 0; i < row.picList.length; i++) {
let param = { name: "", url: "", data: "" };
param.name = row.picList[i].id;
param.url = "/xxx/xxx/xxx?picUrl=" + row.picList[i].picurl;
this.picArr.push(param);
}
}

通过接受进来的数据将已经上传过的图片进行格式化,其中需要包含name、url 两种属性。HTML中需要写入:file-list="picArr"其中picArr就是自动添加进去的图片(格式与手动上传的图片不统一,提交的时候需要再次处理)

4.将文件以formdata的形式进行发送

//发送请求
let params = new FormData()
params.append('file', file.file)
params.append('size', file.file.size) api.submit(params)
.then((res) => {
this.$message.success('上传图片成功')
this.feedbackImg.push(res.data)
})
.catch((err) => {
console.error(err)
})

钩子

1.更改默认请求 http-request

需要将默认请求开启 :auto-upload="true"

// 自定义上传图片
uploadFiles(data) {
console.log(data)
this.formData.fileName = data.file.name //文件名
this.formData.fileType = 'updateNoticeFile'
this.getBase64(data.file).then((resBase64) => {
// 操作。。。
this.fileList.push({
name: this.formData.fileName,
url: resBase64,
})
})
},

2.图片上传前 before-upload

// 上传文件前
uploadBefore(file) {
const isJPG =
file.type === "image/png" ||
file.type === "image/jpg" ||
file.type === "image/jpeg";
const isLt = file.size / 1024 / 1024 < 2;
if (!isJPG) {
this.$message.error("只能上传.jpg/.png/.jpeg格式图片!");
return false;
}
if (!isLt) {
this.$message.error("上传图片大小不能大于等于2MB!");
return false;
}
return true;
},

3.发生改变之后 on-change

//发生改变后
onChange(file, fileList) {
this.hideUpload = this.imgList.length >= this.limitCount;
//操作...
},

4.文件上传成功后 on-success

//上传成功后
onSuccess(response, file, fileList, xhr) {
this.picList = fileList;
},

利用第三个参数fileList保存当前文件列表的状态

5.删除成功后 on-remove

其中imgList为个人自定义添加的图片列表,实际可用fileList替代

// 删除成功后
onRemove(file, fileList) {
// file = JSON.stringify(file)
// 对应内容的索引
Array.prototype.indexOf = function(val) {
for (var i = 0; i < this.length; i++) {
if (this[i] == val) return i;
}
return -1;
};
// 删除对应索引的
Array.prototype.remove = function(val) {
var index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
}; if (file && file.status === "success") {
// 删除成功时候的方法
this.imgList.remove(file);
this.hideUpload = this.imgList.length >= this.limitCount;
}
this.picList = fileList;
},

利用第二个参数 fileList 保存文件列表的状体

样式

隐藏添加按钮

根据动态增加hide类使新增图片按钮动态隐藏

对upload上传文件标签添加动态的class当图片上传至指定个数后将添加按钮隐藏

:class="{ hide: hideUpload }"
// 隐藏新增的按钮
/deep/ .hide .el-upload--picture-card {
display: none;
}

element上传图片组件使用方法|图片回显|格式转换base64的更多相关文章

  1. java图片上传及图片回显1

    目的:选择图片,进行图片回显之后将图片保存到服务器上(PS:没有使用任何插件,样式很丑) 实现方式: js+servlet+jsp的方式来实现 事先准备: 文件上传处理在浏览器中是以流的形式提交到服务 ...

  2. vue element upload图片 回显问题

      beforeUpload (file) { var _this = this; var reader = new FileReader(); reader.readAsDataURL(file); ...

  3. jfinal 后台文件上传(结合上一篇(h5 图片回显))

    前端用了jquery.form.js插件异步提交表单 $("#imgForm").ajaxSubmit();//户主头像 /** * * @description 上传户主头像 * ...

  4. h5 图片回显

    <form method="post" id="imgForm" action ="/hi/holdHead" enctype=&qu ...

  5. aliyun-oss 通过redis来实现跨域上传图片到阿里 OSS并回显进度条

    public class PutObjectProgressListener implements ProgressListener {        private long bytesWritte ...

  6. 使用canvas给图片添加水印, canvas转换base64,,canvas,图片,base64等转换成二进制文档流的方法,并将合成的图片上传到服务器,

    一,前端合成带水印的图片 一般来说,生成带水印的图片由后端生成,但不乏有时候需要前端来处理.当然,前端处理图片一般不建议,一方面js的处理图片的方法不全,二是有些老版本的浏览器对canvas的支持度不 ...

  7. [Python]-opencv-python模块(cv2)-图片读取和格式转换

    python常常用opencv模块来处理图像. import cv2 as cv 读取图片:imread() 默认按照彩色三通道读取: img = cv2.imread(path) 读取灰度图: im ...

  8. 淘淘商城学习笔记 之 上传图片到远程服务器,图片的回显出现的bug

    最近在学习淘淘商城中用到的技术,感觉受益良多,遇到一个比较奇怪的bug调了好久,遂心乐之分享于诸君 bug情况是这样的:在商城的后台上传图片之后图片回显不出来,右键查看链接,发现链接被加了localh ...

  9. ueditor1.4.3jsp版成功上传图片后却回显不出来与在线管理显示不出图片的解决方案

    这是因为路径问题,可以在jsp/config.json这个文件去改路径 通过“imageUrlPrefix”与“imagePathFormat”这两个属性去拼凑路径. “imageUrlPrefix” ...

  10. 如何用input标签上传多个图片并回显

    本文主要记录如何用input标签和jquery实现多图片的上传和回显,不会涉及后端的交互,大概的效果看图 我们从零来做一个这样的demo 第一步: 我们先完善一下我们的页面,默认的input-file ...

随机推荐

  1. C语言爱心表白程序

    #include <stdio.h> #include <math.h> #include <windows.h> #include <tchar.h> ...

  2. Mysql之PXC高可用

    PXC高可用 1.环境准备 pxc1: centos7 10.0.0.7 pxc2: centos7 10.0.0.17 pxc3: centos7 10.0.0.27 pxc4: centos7 1 ...

  3. springboot整合mybatis步骤以及错误集合

    1.首先在springboot项目中的pomx文件引入官方的依赖 <groupId>org.mybatis.spring.boot</groupId> <artifact ...

  4. Oracle数据库允许最大连接数

    1.查看当前的数据库连接数 SQL> select count(*) from v$process ; 2.数据库允许的最大连接数 SQL> select value from v$par ...

  5. mindxdl--common--k8s_utils.go

    // Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.// Package common define co ...

  6. 【云原生 · Kubernetes】部署zookeeper

    个人名片: 因为云计算成为了监控工程师‍ 个人博客:念舒_C.ying CSDN主页️:念舒_C.ying 部署zookeeper 1.1 zookeeper概述 1.2 ZooKeeper服务中操作 ...

  7. VS 新版本无法打开旧项目问题处理

    问题 最近想阅读 WorkflowCore 的源码,苦于代码量巨大,就想将项目回退到 Init Commit 版本 但是在回退版本后,工程内Project 显示已卸载 重新加载后 提示: 不支持 Th ...

  8. EntityUtils MapStruct BeanCopier 数据实体类转换工具 DO BO VO DTO 附视频

    一.序言 在实际项目开发过程中,总有数据实体类互相转换的需求,DO.BO.VO.DTO等数据模型转换经常发生.今天推荐几个好用的实体类转换工具,分别是EntityUtils MapStruct Bea ...

  9. C温故补缺(十五):栈帧

    栈帧 概念 栈帧:也叫过程活动记录,是编译器用来实现过程/函数调用的一种数据结构,每次函数的调用,都会在调用栈(call stack)上维护一个独立的栈帧(stack frame) 栈帧的内容 函数的 ...

  10. Solon v1.11.0 发布,Hello Java

    一个更现代感的 Java 应用开发框架:更快.更小.更自由.没有 Spring,没有 Servlet,没有 JavaEE:独立的轻量生态.主框架仅 0.1 MB. @Controller public ...