在用这块代码前需要在主页面index引入<script src="http://at.alicdn.com/t/font_kfbh2nlnqrrudi.js"></script>

html:

这里需要引入:import EXIF from 'exif-js'  Exif.js 提供了 JavaScript 读取图像的原始数据的功能扩展

   <div v-if="question.question_type==5" class="finnalPicture">{{p_index+}}.{{question.question_title}}<span style="color:red;" v-show="question.required==true">*</span></div>
<div v-if="question.question_type==5" class="addQuestion">
<div class="editArea">
<li v-for="item in imgObj[p_index]" class="imgItem" :style="{backgroundImage: 'url(' + item + ')' }" @click="deleteImg(item,p_index)"></li>
<div class="addImg">
<div class="addImgBtn">
+
<input type="file" @change="onFileChange($event,question.question_id,p_index)">
</div>
</div>
</div>
</div>

js:

data里面需要定义imgObj: {},

onFileChange (e, id, index) {
Indicator.open()
var files = e.target.files || e.dataTransfer.files
if (!files.length) {
return
}
this.createImage(files[], id, index)
},
createImage (file, id, index) {
let orientation =
EXIF.getData(file, function () {
orientation = EXIF.getTag(file, 'Orientation')
})
var reader = new FileReader()
reader.onload = (e) => {
let result = e.target.result
this.compress(result, orientation, (data) => {
let obj = {}
obj.imgpath = data
obj.expert_consult_order_id = this.orderId
this.imgObj[index].push(data)
let imgStr = {
question_id: id,
question_value: this.imgObj[index]
}
this.submitArray[index] = imgStr
let effectimg = {question_id: id, question_value: this.imgList}
this.submitQuestion[index] = effectimg
Indicator.close()
})
}
reader.readAsDataURL(file)
},
compress (img, orientation, next) {
let image = new Image()
image.onload = function () {
let degree =
let width = image.naturalWidth
let height = image.naturalHeight
let maxSide = Math.max(width, height)
let minSide = Math.min(width, height)
if (maxSide > ) {
minSide = minSide / maxSide *
maxSide =
if (width > height) {
width = maxSide
height = minSide
} else {
width = minSide
height = maxSide
}
}
let canvas = document.createElement('canvas')
let cxt = canvas.getContext('2d')
canvas.width = width
canvas.height = height
cxt.fillstyle = '#fff'
if (orientation !== '' && orientation !== ) {
switch (orientation) {
case :
degree =
width = -width
height = -height
break
case :
canvas.width = height
canvas.height = width
degree =
height = -height
break
case :
canvas.width = height
canvas.height = width
degree =
width = -width
break
}
}
cxt.rotate(degree * Math.PI / )
cxt.drawImage(image, , , width, height)
next(canvas.toDataURL('image/jpeg', 0.8))
}
image.src = img
},
deleteImg (item, pindex) {
MessageBox.confirm('您想删除这张图片吗?').then(action => {
let index = this.imgObj[pindex].indexOf(item)
this.imgObj[pindex].splice(index, )
})
}

css:

.finnalPicture{
color: #000000;
font-size: torem(14px);
background-color: #eeeeee;
padding: torem(8px) torem(15px);
}
.editArea{
background-color: #ffffff;
padding: torem(15px);
display: flex;
flex-flow: row wrap;
.question{
box-sizing: border-box;
display: block;
width: %;
font-size: torem(14px);
line-height: torem(20px);
padding: torem(5px) torem(15px);
@include border(all);
border-radius: torem(5px);
}
.imgItem{
display: inline-block;
height: torem(60px);
width: torem(60px);
background-size: % %;
margin: torem(10px) torem(10px) ;
}
.addImg{
line-height: torem(60px);
.addImgBtn{
display: inline-block;
height: torem(60px);
width: torem(60px);
position: relative;
border: 1px dashed #dddddd;
color: #dddddd;
text-align: center;
font-size: torem(30px);
input{
position: absolute;
top: ;
left: ;
height: %;
width: %;
opacity: ;
}
}
span{
padding: torem(15px);
font-size: torem(14px);
color: #;
}
}
}

vue上传图片的更多相关文章

  1. vue 上传图片视频组件,可拍照选择照片,解决苹果手机拍照旋转问题

    1.创建组件components > uploadImg > index.vue <template> <input type="file" name ...

  2. Vue上传图片预览组件

    父组件: <template> <div> <h4>基于Vue.2X的html5上传图片组件</h4> <div style="widt ...

  3. axios上传图片(及vue上传图片到七牛))

    浏览器上传图片到服务端,我用过两种方法: 1.本地图片转换成base64,然后通过普通的post请求发送到服务端. 操作简单,适合小图,以及如果想兼容低版本的ie没办法用此方法 2.通过form表单提 ...

  4. Vue 上传图片压缩 的问题

    前言 也是很少来写博客了,也是赖吧,哈哈 最近新的进度里有上传图片太大,需要前台进行图片压缩问题,然后查阅了相关资料 上传图片大于100* 1024 的用canvas 来压缩来解决 然后IOS拍照上传 ...

  5. vue 上传图片到阿里云(前端直传:不推荐)

    为何要这样做:减轻后端数据库压力(个人觉得于前端没啥用,谁返回来都行) 代码部分: <template> <div class="upLoad"> < ...

  6. vue上传图片 base64+canvas压缩图片

    这是先将图片 base64转码 在拿canvas压缩的

  7. vue上传图片到服务器

    https://blog.csdn.net/qq_29712995/article/details/78839093(copy) HTML代码: <input accept="imag ...

  8. vue 上传图片 input=file

    一.逻辑 点击li触发事件chooseImage 即触发input标签事件photoChange input标签事件photoChange file返回的是如下变量 模拟上传表单方法 执行上传 二.代 ...

  9. vue上传图片的3种方式

    https://blog.csdn.net/q3254421/article/details/88250968?utm_medium=distribute.pc_relevant.none-task- ...

随机推荐

  1. Python里面search()和match()的区别

    转自https://www.cnblogs.com/aaronthon/p/9435967.html match()函数只检测字符串开头位置是否匹配,匹配成功才会返回结果,否则返回None searc ...

  2. Spring的事务管理1

    事务的回顾: 事务:逻辑上的一组操作,组成这组事务的各个单元,要么全部成功,要么全部失败 事务的特性:ACID 原子性(Atomicity):事务不可分割 一致性(Consistency):事务执行前 ...

  3. 部署tinyproxy代理服务

    #安装依赖 yum install asciidoc #下载 wget https://github.com/tinyproxy/tinyproxy/releases/download/1.8.4/t ...

  4. Error: client: etcd cluster is unavailable or misconfigured; error #0: dial tcp 127.0.0.1:4001: getsockopt: connection refused

    配置docker网络flannel时,配置etcd的key的时候出现以下错误 Error:  client: etcd cluster is unavailable or misconfigured; ...

  5. 转://通过udev创建ASM共享磁盘(RAC)

    OS:RedHat EL6.0 Oracle:   Oracle 11gR2 在Oracle 11gR2,构建RAC时可以通过ASM创建asm disk,但是需要安装asmlib相关软件:对于RedH ...

  6. Redo丢失的4种情况及处理方法

    这篇文章重点讨论Redo丢失的几种情况,及每种情况的处理方法. 一.说明:1.以下所说的当前日志指日志状态为CURRENT,ACTIVE,非当前日志指日志状态为INACTIVE2.不用考虑归档和非归档 ...

  7. Python 在 Terminal 中的自动补全

    为了在 Terminal 中使用 Python 更加方便,在 home 目录下添加脚本 .pythonstartup,内容如下, 然后在 .bashrc 中添加 export PYTHONSTARTU ...

  8. 工具 docker

    环境配置 软件+环境 虚拟机 资源占用 容器  进程隔离 image(镜像)文件 容器文件 docker hub:仓库 docker container run image_name 挂载: 参考链接 ...

  9. Linux中断管理 (1)Linux中断管理机制

    目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...

  10. Linux每天一个命令:grep

    grep (缩写来自Globally search a Regular Expression and Print) 是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出 ...