在用这块代码前需要在主页面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. Java strictfp

    strictfp关键字 用于强制Java中的浮点计算(float或double)的精度符合IEEE 754标准. 不使用strictfp:浮点精度取决于目标平台的硬件,即CPU的浮点处理能力. 使用s ...

  2. 关于pycharm中使用charts无法显示图表的问题(属于个人粗心问题)

    在练习用charts库对爬取的数据进行数据分析并图表化时遇到一个问题,无法显示图表,如下: 经过重装charts库等一顿折腾后,终于发现一个问题,看这里: def data_gen(type): le ...

  3. Mac中selenium使用出现错误

    解决方案是: 首先通过brew 安装 $ brew install geckodriver 然后设置配置文件~/.bash_profile文件 export PATH=$PATH:/path/to/g ...

  4. Quartz 的DisallowConcurrentExecution

    DisallowConcurrentExecution注解是指:一个JobKey对应的JobDetail实例不运行并发执行,而不是说你继承Job之后的子类不允许并发执行.

  5. Python:Day40 html

    URL包括三个部分:协议.域名.路径 htyper text markup language (html)  即超文本标记语言 前端一共包括三个内容:html.css.js html做为基础,让CSS ...

  6. 【vue】vue +element 搭建项目,在使用InputNumber 计数器时遇到的问题

    自己遇到的坑: InputNumber 计数器的change事件定义时如果不传入参数value,会产生this.num不同步的问题 <template> <el-input-numb ...

  7. hyperledge工具-cryptogen

    参考:http://baijiahao.baidu.com/s?id=1596614770784685300&wfr=spider&for=pc cryptogen是Hyperledg ...

  8. leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II

    74. Search a 2D Matrix 整个二维数组是有序排列的,可以把这个想象成一个有序的一维数组,然后用二分找中间值就好了. 这个时候需要将全部的长度转换为相应的坐标,/col获得x坐标,% ...

  9. face detection[Face R-CNN]

    face r-cnn是腾讯ai实验室的作品,而且登录过腾讯ai实验室官网,发现果然硕果累累,不得不佩服. 1 引言 人脸检测虽然相对之前有了不小的进步,可是还是因为真实世界中人脸图像的明显变化导致仍然 ...

  10. JS中AOP的实现和运用

    在编写js的时候,我们有时会遇到针对某种场景做处理,比如在方法开始的时候校验参数,执行方法前检查权限,或是删除前给出确认提示等等.这些校验方法.权限检测.确认提示,规则可能都是相同的,在每个方法前去调 ...