vue 图片上传组件
来源:互联网 发布:java split参数 编辑:程序博客网 时间:2024/06/05 09:19
vue图片上传组件
vue图片上传组件,实现批量上传和单张上传功能,控制图片大小,数量,以及压缩处理,利用了mint-ui的提示框
监听两个参数,分别是MAX控制最多上传的数量,multiple控制是否批量上传,self.$emit(‘fileChange’, {base64, file, files});将处理后的数据通过自定义事件传递给父组件,父组件获取到压缩处理后的文件就可以使用AJAX等技术post给后台实现文件上传功能
<template> <div> <input ref="file" type="file" class="file-input" accept="image/*" :multiple="multiple" @change="change"/> </div></template><script> import {Indicator, MessageBox} from 'mint-ui'; // M设定可以上传图片的大小 const M = 7; const UNIT = 1024; const MAX_SIZE = M * UNIT * UNIT; /** * @author xx * * @desc 图片上传 * @module @components/updatefile * * @example * import {updatefile} from @components * Vue.component(Updatefile.name, Updatefile) * <updatefile :multiple="multiple" * :max="max" * @refDom="getDom" * @fileChange="fileChange"> * </updatefile> */ export default { name: 'updatefile', /** * @member {Object} * @name props * @property {Boolean} multiple 是否可以同时上传多个文件 * @property {Number} max 最多一次可以上传多少张 */ props: { multiple: Boolean, max: Number }, data() { return { }; }, mounted() { this.getDom(); }, methods: { // 获取dom元素外部点击触发 getDom() { let file = this.$refs.file; this.$emit('refDom', file); }, // 图片数量是否超过max张 isOutNumber(files) { const MAX = this.max; if (files.length > MAX) { MessageBox({ title: '提示', message: `一次最多允许上传${MAX}张!` }); return true; } else { return false; } }, change(e) { let files = e.target.files; // 如果超过设定的张数张,提示 if (this.isOutNumber(files)) { return; } let len = files.length; for (let i = 0; i < len; i++) { if (window.FileReader && files[i]) { if (files[i].size <= MAX_SIZE) { Indicator.open(); // 读取文件转化图片base64 let fr = new FileReader(); let self = this; fr.onload = (e) => { let w = 300; let h = 300; let file = files[i]; let compressPic = function(base64) { // 将压缩处理后的图片传递出去 self.$emit('fileChange', {base64, file, files}); }; self.canvasDataUrl(e.target.result, w, h, compressPic); }; fr.onerror = (e) => { Indicator.close(); }; fr.readAsDataURL(files[i]); } else { MessageBox({ title: '警告', message: `${files[i].name}图片尺寸过大,图片请小于7M!` }); } } } }, // 利用canvas压缩图片 canvasDataUrl(data, w, h, callback) { let newImg = new Image(); newImg.src = data; let imgWidth, imgHeight; newImg.onload = () => { let img = document.createElement('img'); img.src = newImg.src; imgWidth = img.width; imgHeight = img.height; let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); if (imgWidth > imgHeight) { imgWidth = w * imgWidth / imgHeight; imgHeight = h; } else { imgHeight = h * imgHeight / imgWidth; imgWidth = w; }; canvas.width = imgWidth; canvas.height = imgHeight; ctx.clearRect(0, 0, w, h); // 处理png格式图片背景变黑的问题 ctx.fillStyle = '#fff'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.drawImage(img, 0, 0, imgWidth, imgHeight); let rate = 0.7; let base64 = canvas.toDataURL('image/jpeg', rate); callback(base64); }; } } };</script>
阅读全文
0 0
- vue 图片上传组件
- vue上传图片组件
- vue上传图片组件编写
- vue上传图片组件编写
- Vue上传图片预览以及删除的vue组件
- vue上传图片组件(支持拖拽文件夹上传)
- require.js+vue+vue-router+vue-resource开发微信上传图片组件
- vue 上传图片
- vue上传单张图片
- vue 上传图片
- 基于Vue + Node.js + MongoDB的图片上传组件,实现图片的预览和删除
- 从0开始做一个的Vue图片/ 文件选择(上传)组件[基础向]
- 从0开始做一个的Vue图片/ 文件选择(上传)组件[基础向]
- vue移动端图片上传
- vue上传多张图片
- input_file上传图片(vue)
- 无组件上传图片
- 求图片上传组件
- xml文件出现警告:No grammar constraints (DTD or XML Schema) referenced in the document.
- Angular 4入门教程系列:9:TypeScript:ECMAScript之前世今生
- mybatis 一对多
- #前端笔记#导入js等静态文件时报错:No mapping found for HTTP request with URI
- linux shell之watch
- vue 图片上传组件
- 《数据库系统概念》1-数据抽象、模型及SQL
- mybatis下opening session时空指针异常
- ci框架 开发环境下面, php出现错误,直接返回500,而不是提示错误
- 11月6号练习和心得
- 剑指offer——平衡二叉树
- jQuery学习笔记之empty()
- RL-TCPnet之TCP服务器(enc28j60)
- 自学QT,点滴记录