Vue 图片压缩上传: element-ui + lrz

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

步骤

  • 安装依赖包 npm install --save lrz
  • main.js里引入 import lrz from 'lrz'
  • 封装 compress函数
  • 封装上传组件 upload-image
  • 在 vue 文件中 使用

封装 compress函数

// eslint-disable

/** @format */
// base64编码转File
export function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
} // main function.
export function compress(file, fileList, that, reference) {
if (file.status != 'ready') {
return;
}
let index = reference.uploadFiles.indexOf(file); // jpg/jpeg/bmp/gif/png
let isJPG =
file.raw.type === 'image/jpg' ||
file.raw.type === 'image/jpeg' ||
file.raw.type === 'image/png' ||
file.raw.type === 'image/gif' ||
file.raw.type === 'image/bmp' const isLt20M = file.size / 1024 / 1024 < 20;
if (!isJPG) {
that.$message.error('不支持的格式,请上传jpg/jpeg/bmp/gif/png格式的图片!');
fileList.splice(fileList.indexOf(file), 1);
return;
}
if (!isLt20M) {
that.$message.error('上传图片大小不能超过 20MB!');
fileList.splice(fileList.indexOf(file), 1);
return;
}
if (isJPG & isLt20M) {
if (isLt2M(file)) {
// pdf不压缩
submit(reference, file);
} else {
let options = {};
lrz(file.raw, options)
.then(rst => {
let tempFile = dataURLtoFile(rst.base64, rst.origin.name);
tempFile.uid = rst.origin.uid;
reference.uploadFiles[index].raw = tempFile;
reference.uploadFiles[index].size = rst.fileLen;
})
.catch(function (error) {
// 失败时执行
if (error) {
// IE报错
if (error.name == 'TypeError' && error.message == '对象不支持此操作') {
that.$message.error('当前浏览器不支持上传大于2M的文件,请更换浏览器!');
}
// 图片格式问题
else if ((error + '').indexOf('加载图片文件失败') > -1) {
that.$message.error('系统未能识别当前上传图片,请更换!');
}
}
fileList.splice(fileList.indexOf(file), 1);
})
.always(function () {
//不管成功或失败都会执行
submit(reference, file);
});
}
}
}
// 判断文件大小是否小于2M
function isLt2M(file) {
if (file.raw.type === 'application/pdf') {
return true;
} else {
return file.size / 1024 / 1024 < 2;
}
} function submit(reference, file) {
let submitFlag = true;
if (reference.multiple) {
for (let item of reference.uploadFiles) {
if (item.status != 'ready') {
continue;
}
if (!isLt2M(item)) {
submitFlag = false;
break;
} else {
submitFlag = true;
}
}
} else {
if (!isLt2M(file)) {
submitFlag = false;
} else {
submitFlag = true;
}
}
if (submitFlag) {
reference.submit();
return;
}
}

封装上传组件upload-image

<template>
<div class="upload-component-container">
<div class="upload-section">
<!-- 上传主体 -->
<el-row>
<el-upload
ref="imageUploadRef"
:headers="token"
:action="uploadUrl"
:auto-upload="false"
list-type="picture-card"
:file-list="imageList"
:on-change="resize"
:on-remove="handleRemove"
:on-preview="handlePreview"
:on-success="handleSuccess"
:on-error="handleError"
:on-exceed="handleExceed"
:on-progress="handleProgress"
:class="imageList.length >= maxCount ? 'uploaded-none':''"
:limit="maxCount"
>
<div class="icon-container" v-loading="loading">
<i class="el-icon-plus upload-icon"></i>
</div>
</el-upload>
</el-row>
<!-- 上传描述 -->
<el-row v-if="needDesc">
<div class="upload-text-desc" :style="descStyle">{{uploadDesc}}</div>
</el-row>
</div>
<!-- 查看图片 -->
<el-dialog
width="50%"
:modal="modal"
@close="imageVisible=false"
:modal-append-to-body="false"
:visible.sync="imageVisible"
>
<img width="100%" :src="dialogImageUrl" alt />
</el-dialog>
</div>
</template>
<script>
import { getBaseUrl, uuid as getUUId } from '@/utils/basic/' // 引入项目的api前缀
import { getToken } from '@/utils/browser' // 引入token
import { compress } from '@/utils/image/compress' // 引入上一步封装的压缩函数
import lodash from 'lodash'
export default {
name: 'UploadImage',
props: {
modal: { type: Boolean, default: () => { false } },
maxCount: { type: Number, required: true },
uploadDesc: { type: String },
initShowImages: { type: [Array, String], required: true },
singleFile: { type: Boolean, default: false },
needDesc: { type: Boolean, default: true },
handleChange: { type: Function, required: true },
descStyle: { type: Object, default: () => { null } }
},
data() {
return {
uploadUrl: getBaseUrl() + '/api/base/uploadImg',
token: { Authorization: getToken(), 'X-Trace-ID': getUUId() },
imageList: [],
loading: false,
imageVisible: false,
dialogImageUrl: ''
}
},
methods: {
// 启用压缩
resize(file, fileList) {
compress(file, fileList, this, this.$refs.imageUploadRef)
},
// 上传成功
handleSuccess(res, f, fList) {
if (res.status === 1 && res.result.length > 0) {
if (res.result[0].url) {
this.imageList.push({
url: res.result[0].url,
// 兼容saas的初始 attachmentUrl 属性
attachmentUrl: res.result[0].url //
})
} else {
this.imageList.push({
url: fList[0].url,
// 兼容saas的初始 attachmentUrl 属性
attachmentUrl: fList[0].url //
})
}
}
// 根据接收类型将结果发送到父组件
if (this.singleFile == true) {
this.handleChange(this.imageList[0].url)
} else {
this.handleChange(this.imageList)
}
this.loading = false
},
// 删除事件
handleRemove(res) {
this.imageList = lodash.filter(this.imageList, item => {
if (res.response) {
return item.url !== res.response.result[0].url
} else {
return item.url !== res.url
}
})
// 根据接收类型将结果发送到父组件 => 单个文件
if (this.singleFile == true) {
this.handleChange('')
} else {
this.handleChange(this.imageList)
}
this.$forceUpdate()
}, // 最大数
handleExceed() {
this.$message({ type: 'warning', message: `最多上传${this.maxCount}张图片` })
},
// 上传错误
handleError(err, file, fileList) {
this.$message({ message: err.data.msg, type: 'error' })
},
// 查看图片
handlePreview(file) {
this.dialogImageUrl = file.url
this.imageVisible = true
},
handleProgress() {
this.loading = true
}
},
mounted() {
// case: string(logo)
if (this.singleFile == true) {
// 非空值
if (this.initShowImages) {
this.imageList.push({ url: this.initShowImages })
} else {
this.imageList = []
}
} else {
// 列表文件
this.imageList = this.initShowImages
}
},
watch: {
initShowImages(val) {
// case: string(logo)
if (this.singleFile == true) {
// 非空值
if (val) {
this.imageList = [{ url: val }]
} else {
this.imageList = []
}
} else {
// 列表文件
this.imageList = val
}
}
}
}
</script>
<style lang="scss">
.upload-component-container {
.upload-section {
background: #ffffff;
border-radius: 4px;
padding-top: 2px;
.uploaded-none {
.el-upload--picture-card {
display: none;
}
}
.el-upload-list--picture-card .el-upload-list__item {
width: 60px;
height: 60px;
}
.el-upload--picture-card {
width: 60px;
height: 60px;
line-height: 125px;
i {
font-size: 20px !important;
}
}
.icon-container {
line-height: 2.5em;
padding-top: 12px;
}
.el-icon-plus:before {
// background: rgb(77, 227, 193);
color: #000;
border-radius: 12px;
}
}
.upload-text-desc {
font-size: 12px;
color: #939393;
margin-top: 4px;
margin-bottom: 5px;
}
}
</style>

在 vue 文件中使用

<template>
<div class="contract-other-edit-container">
<el-row>
<el-col :span="12">
<upload-image
:modal="false"
:maxCount="5"
uploadDesc="格式要求:支持jpg/jpeg/bmp/gif/png格式图片,大小不超过20MB,多上传5张图片."
:handleChange="handleUploadImage"
:initShowImages="imageList"
/>
</el-col>
</el-row>
</div>
</template> <script>
import UploadImage from '@/components/element-ui/upload/image' // 组件的位置 export default {
name: 'other-edit',
components: { UploadImage },
data() {
return {
imageList: []
}
},
methods: {
handleUploadImage(files) {
this.imageList = files
}
}
}
</script>
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: vue

“Vue 图片压缩上传: element-ui + lrz” 的相关文章

ubuntu中删除文件的命令 - rm

1、rmdir删除整个文件夹 使用rmdir命令删除一个目录时需要离开删除的目录并删除的目录必须是空的否则会删除失败。 rmdir test //删除空文件夹test为文件夹名 2、rm命令删除文件或目录删除后不能恢复 使用rm命令删除文件一旦删除成功就无法恢复了。 在删除过程中可以使...

C语言课程设计题目介绍(10个标准题目)

《C语言课程设计》 1、学生成绩管理系统 学生数据由学号、姓名、班级、三门课数学、英语、计算机的成绩和平均成绩构成。 实现功能包括 1添加学生的记录 2查询学生分别按学号和姓名 3对学生数据排序分别按平均成绩和计算机成绩的降序 4删除学生记录 5修改学生记录 6班级成绩分析各...

Vue ElementUI在el-table中怎么使用el-popover - 开发技术

本文小编为大家详细介绍“Vue ElementUI在el-table中怎么使用el-popover”,内容详细,步骤清晰,细节处理妥当,希望这篇“Vue ElementUI在el-table中怎么使用el-popover”文章能帮助大家解决疑惑,下...

java中枚举使用及遍历(一)

具体代码如下;/** * @ClassName: MccCodeEnum * @Description: mccCode 枚举类 * @author tianweichang * @date 2015年6月26日 上午13:32:52 */ public enum MccCodeEnum...

IDL编程语言的代码,打开文件、读文件、写文件

pro read_write_file, filename, output_filename ;打开输入文件 openr, 1, filename, /get_lun ;读取文件内容 content = '' while(not eof(1)) do be...

Node的进程管理工具pm2怎么使用 - web开发

本篇内容介绍了“Node的进程管理工具pm2怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! PM2简介PM2是一个内建了负载均衡器的...