elementUI+react

布局

         <Dialog
title="充值"
visible={ dialogVisible }
onCancel={ () => this.setState({ dialogVisible: false }) }
>
<Dialog.Body>
<Form labelWidth="120" ref={ e => {this.form=e} } model={ form } rules={ rules }>
<Form.Item label="支付凭证" prop="voucher">
<div className="my-upload">
<input className="upload-input" type="file" id="input" name="file1" onChange={ this.inputfile } />
{ voucher ? <img src={ voucher } className="avatar" alt="" /> : <i className="el-icon-plus avatar-uploader-icon" /> }
</div>
</Form.Item>
</Form>
</Dialog.Body>
<Dialog.Footer className="dialog-footer">
<Button onClick={ () => this.setState({ dialogVisible: false }) }>{'取 消'}</Button>
<Button type="primary" onClick={ this.saveContent } loading={ btnLoading }>{'确 定'}</Button>
</Dialog.Footer>
</Dialog>

完整方法:

  beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG) {
Message('上传头像图片只能是 JPG 格式!')
}
if (!isLt2M) {
Message('上传头像图片大小不能超过 2MB!')
}
return isJPG && isLt2M
}
  // input onchange上传方法
inputfile =()=> {
const file1 = document.querySelector('#input').files[0]
// const flag = this.beforeAvatarUpload(file1)
// if(!flag){
// return false
// }
console.log(file1)
const that=this
if(file1){
var reader = new FileReader()
// 图片文件转换为base64
reader.readAsDataURL(file1)
reader.onload = function(){
// 显示图片
// document.getElementById('file1_img').src = this.result
// that.setState({
// voucher:this.result
// })
that.dealImage(this.result, 800, that.printing)
}
}
}
  //回调方法
printing = base64 => {
// console.log('压缩后', base64.length / 1024)
this.setState({
voucher: base64
})
}
//压缩方法
dealImage = (base64, w, callback) => {
var newImage = new Image()
//压缩系数0-1之间
var quality = 0.6
newImage.src = base64
newImage.setAttribute('crossOrigin', 'Anonymous') //url为外域时需要
var imgWidth, imgHeight
newImage.onload = function () {
imgWidth = this.width
imgHeight = this.height
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d')
if (Math.max(imgWidth, imgHeight) > w) {
if (imgWidth > imgHeight) {
canvas.width = w
canvas.height = w * imgHeight / imgWidth
} else {
canvas.height = w
canvas.width = w * imgWidth / imgHeight
}
} else {
canvas.width = imgWidth
canvas.height = imgHeight
quality = 0.6
}
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(this, 0, 0, canvas.width, canvas.height)
var ba = canvas.toDataURL('image/jpeg', quality) //压缩语句
// 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间,请加以下语句,quality初始值根据情况自定
// while (base64.length / 1024 > 150) {
// quality -= 0.01;
// base64 = canvas.toDataURL("image/jpeg", quality);
// }
// 防止最后一次压缩低于最低尺寸,只要quality递减合理,无需考虑
// while (base64.length / 1024 < 50) {
// quality += 0.001;
// base64 = canvas.toDataURL("image/jpeg", quality);
// }
callback(ba) //必须通过回调函数返回,否则无法及时拿到该值
}
}

上传base64图片并压缩的更多相关文章

  1. 上传base64图片到七牛云前端遇到的坑

    介意前端普通引入七牛云SDk上传图片到七牛云需要多个js,所以才有了base64的上传方式,简化操作,(懒.) 七牛云官方文档如下 https://developer.qiniu.com/kodo/k ...

  2. 基于vue上传base64图片,通过canvas压缩base64

    其实和vue关系不大,和我们之前做上传压缩性质是一样的 当然下面的代码是没有处理ios横屏拍照的bug的 有兴趣的可以多搜一下  网上都有相应的解答 .. var that = this if (e. ...

  3. 上传base64图片至七牛云,并返回图片link

    https://developer.qiniu.com/kodo/kb/1326/how-to-upload-photos-to-seven-niuyun-base64-code

  4. Vue directive自定义指令+canvas实现H5图片压缩上传-Base64格式

    前言 最近优化项目-手机拍照图片太大,回显速度比较慢,使用了vue的自定义指令实现H5压缩上传base64格式的图片 canvas自定义指令 Vue.directive("canvas&qu ...

  5. vue实现PC端调用摄像头拍照人脸录入、移动端调用手机前置摄像头人脸录入、及图片旋转矫正、压缩上传base64格式/文件格式

    进入正题 1. PC端调用摄像头拍照上传base64格式到后台,这个没什么花里胡哨的骚操作,直接看代码 (canvas + video) <template> <div> &l ...

  6. weui上传多图片,前端压缩,base64编码

    记录一下在做一个报修功能的心路历程,需求功能很简单,一个表单提交,表单包含简单的文字字段以及图片 因为使用的是weui框架,前面的话去找weui的表单和图片上传组件,说实话,weui的组件写的还不错, ...

  7. 移动端图片上传解决方案localResizeIMG先压缩后ajax无刷新上传

    现在科技太发达,移动设备像素越来越高,随便一张照片2M+,但是要做移动端图片上传和pc上略有不同,移动端你不能去限制图片大小,让用户先处理图片再上传,这样不现实.所以理解的解决方案就是在上传先进行图片 ...

  8. 前端上传 base64 编码图片到七牛云存储

    参考文档 如何上传base64编码图片到七牛云 调试过程 文档中分别有 java 和 html 的 demo,可以根据文档示例调试. 下面是我调试的过程,可以作为参考,特别注意的是,如果需要给文件起名 ...

  9. 上传base64格式的图片到服务器

    上传base64格式的图片到服务器 /**bash64上传图片 * @param $base64 图片的base64数据 * @param $path 保存路径 */ function base64_ ...

随机推荐

  1. 【问题】如何在Linux与Windows间共享文件

    实验环境 Linux LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS L ...

  2. Python模块(导入,内置,自定义,开源)

    目录: 模块介绍 自定义模块 内置模块 开源模块 一.模块 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用p ...

  3. 第一次使用Git(常用的dos命令整理)

    在使用git的过程中,有许多dos命令也要会用才行 Git 工具分类 命令行 bash.cmd.power shell GUI Git GUI.GitHub Desktop IDE 集成 Visual ...

  4. ThinkPHP模型中的HAS_ONE,BELONG_TO,HAS_MANY实践

    因为很熟悉DJANGO,所以对TP,要慢慢适应. 1,SQL文件 /* Navicat MySQL Data Transfer Source Server : localhost_3306 Sourc ...

  5. [转载]Fiddler界面详解

    转载地址:http://www.cnblogs.com/chengchengla1990/p/5681775.html Statistics 页签 完整页签如下图: Statistics 页签显示当前 ...

  6. C++学习 ---- 系列文章

    C++ 第十二课 其它标准C函数 C++ 第十一课 标准c内存函数 C++ 第十课:标准c时间与日期函数 C++ 第九课 标准c数学函数 C++ 第八课 标准c字符和字符串 C++ 第七课 标准 C ...

  7. 讲解Flume

    Spark Streaming通过push模式和pull模式两种模式来集成Flume push模式:Spark Streaming端会启动一个基于Avro Socket Server的Receiver ...

  8. django-签名加密模块It's dangerous--加密token

    https://juejin.im/entry/56b30250df0eea0054375e1d 安装 pip install itsdangerous 使用 from itsdangerous im ...

  9. ubuntu18.04 + python3 安装pip3

    最近在学习python 网络爬虫,正好接触到python的requests模块 我的开发环境是ubuntu18.04+python3,这个系统是默认自带了python3,且版本是python 3.6. ...

  10. hdu4767 Bell——求第n项贝尔数

    题意 设第 $n$ 个Bell数为 $B_n$,求 $B_n \ mod  \ 95041567$.($1 \leq  n  \leq  2^{31}$) 分析 贝尔数的概念和性质,维基百科上有,这里 ...