vue 图片上传
功能说明:
1、调用手机拍照功能
2、调用相册功能
3、图片上传功能
4、图片预览功能
5、图片删除功能
关键点:
、input 新增multiple
、accept="image/*处理IOS、安卓手机启用摄像头
、根据createObjectURL处理图片预览
、formData构造函数创建空对象
、通过formData.getAll(),把文件流数据转为数组
组件示例图

组件代码 upload_img.vue
<template>
<div class="content">
<div class="imgBox">
<div class="uploadBox" v-for="(value, key) in imgs" :key="'uploadBox' + key">
<img :src="getObjectURL(value)" alt="" class="uploadImg">
<img src="../../assets/images/icon_x.png" alt="" class="closeImg" @click='delImg(key)'>
</div>
<div class="inputButton">
<img src="../../assets/images/icon_photo.png" alt="" class="addImg">
<input type="file" class="upload" @change="addImg" ref="inputer" multiple
accept="image/*"/>
</div>
<div class="submitTask" @click="submit">
上传图片
</div>
</div>
</div>
</template>
js:
export default ({
name: 'uploadPicture',
data: function() {
return {
formData: new FormData(),
imgs: {},
imgLen: ,
txtVal: ,
desc: ''
}
},
created() {
},
methods: {
descInput() {
// this.txtVal = this.desc.length;
},
addImg(event) {
let inputDOM = this.$refs.inputer
// 通过DOM取文件数据
this.fil = inputDOM.files
// console.log(inputDOM.files)
let oldLen = this.imgLen
for (let i = ; i < this.fil.length; i++) {
let size = Math.floor(this.fil[i].size / )
if (size > * * ) {
alert('请选择5M以内的图片!')
return false
}
this.imgLen++
this.$set(this.imgs, this.fil[i].name + '?' + new Date().getTime() + i, this.fil[i])
// console.log(this.imgs)
}
console.log('this.fil', this.fil)
// this.imgs = this.fil
},
getObjectURL(file) {
var url = null
if (window.createObjectURL !== undefined) { // basic
url = window.createObjectURL(file)
} else if (window.URL !== undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file)
} else if (window.webkitURL !== undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file)
}
return url
},
delImg(key) {
this.$delete(this.imgs, key)
this.imgLen--
},
// 提交上传图片
submit() {
for (let key in this.imgs) {
let name = key.split('?')[]
this.formData.append('file', this.imgs[key])
}
this.formData.getAll('file')
console.log('this.formData', this.formData)
this.$refund.upload(this.formData)
}
}
})
</script>
说明:
图片上传完成后,使用getObjectURL来作预览功能处理。
温馨提示:
formData.getAll最后返回来的是一个数组
样式:
<style lang="less">
// 外部盒子样式
.content {
width: %;
padding: .16rem !important;
.imgBox {
display: flex;
display: -webkit-flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
flex-wrap: wrap;
}
} // 预览图片样式
.uploadBox {
position: relative;
width: %;
text-align: left;
margin-right: .1rem;
margin-bottom: .05rem;
// 预览样式
.uploadImg {
width: %;
height: %;
display: block;
overflow: hidden;
}
// 删除按钮
.closeImg {
width: .2rem;
height: .2rem;
position: absolute;
top: %;
right: %;
}
}
// 上图图片icon
.inputButton {
position: relative;
display: block;
width: .2rem;
height: .2rem;
// 上传图片样式
.addImg {
display: block;
width: .2rem;
height: .2rem;
}
// input样式
.upload {
width: .2rem;
height: .2rem;
opacity: ;
position: absolute;
top: ;
left: ;
z-index: ;
}
}
// 提交按钮
.submitTask {
margin: auto;
background: #EF504D;
width: %;
height: .3rem;
color: #fff;
font-size: .16rem;
line-height: .3rem;
text-align: center;
border-radius: .1rem;
margin-top: .8rem;
}
</style>
欢迎关注公众号,进一步技术交流:

vue 图片上传的更多相关文章
- vue图片上传组件
前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...
- vue 图片上传功能
这次做了vue页面的图片上传功能,不带裁剪功能的! 首先是html代码,在input框上添加change事件,如下: <ul class="clearfix"> ...
- vue图片上传的简单组件
<template> <div class="rili" id="rili"> <div class="updel&qu ...
- 保姆级SpringBoot+Vue图片上传到阿里云OSS教程
小二是新来的实习生,作为技术 leader,我给他安排了一个非常简单的练手任务,把前端 markdown 编辑器里上传的图片保存到服务器端,结果他真的就把图片直接保存到了服务器上,这下可把我气坏了,就 ...
- vue图片上传
之前花了两个月用vue做了一个建筑照片我的webApp,前端就我一人,负责用vue写页面对接接口,后台一个程序员负责写接口,嵌套个安卓ios的壳.搞的是风风火火,过程也是很心累,大多不在技术,在于所谓 ...
- vue图片上传到七牛云
代码: <template> <div class="upload-info"> <div> <el-upload class=" ...
- vue图片上传及java存储图片(亲测可用)
1.前言 在使用elementui的upload组件时,我一直无法做到上传的图片和其他数据一起提交.单纯的上传文件,java的存储图片的方式也有局限性. 我知道的后端保存图片有两种方式:一种是直接存储 ...
- 后台管理系统之“图片上传” --vue
图片上传(基于vue) 相信上传图片是所有系统必备的功能吧,工作中的第一个管理系统就在上传图片的功能上卡顿了一整天. 当时用的elementUI组件,但是由于样式和设计图样式差别较大再加上原生相较好理 ...
- vue+axios实现移动端图片上传
在利用vue做一些H5页面时,或多或少会遇到有图片上传的操作,主要是运用html5里面的input[type=file]来实现,传递到后端的数据是以二进制的格式传递,所以上传图片的请求与普通的请求稍微 ...
随机推荐
- 巧用Ajax的beforeSend 提高用户体验--防止重复数据
巧用Ajax的beforeSend 提高用户体验 jQuery是经常使用的一个开源js框架,其中的$.ajax请求中有一个beforeSend方法,用于在向服务器发送请求前执行一些动作.具体可参考jQ ...
- opencv-02--图像的邻域操作
图像的邻域操作 很多时候,我们对图像处理时,要考虑它的邻域,比如3*3是我们常用的,这在图像滤波.去噪中最为常见,下面我们介绍如果在一次图像遍历过程中进行邻域的运算. 下面我们进行一个简单的滤波操作, ...
- 配置vue 多页面
安装vue 1. 全局安装 vue-cli环境 npm install --global vue-cli 2. 创建一个基于webpack模板的新项目 vue init webpack my-proj ...
- [JZOJ5465]道路重建--边双缩点+树的直径
题目链接 lueluelue 分析 这鬼题卡了我10发提交,之前做过一道类似的题目:https://rye-catcher.github.io/2018/07/09/luogu%E9%A2%98%E8 ...
- Python练习_数据类型_day5
1. 1.作业 1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2&qu ...
- oracle学习笔记:update一整列 关联更新
普通的 update 都是根据条件来对部分列的内容进行修改,用法如下: update temp_cwh_table set name = 'xxx' where id = 1; 假设现在有2张表:A. ...
- Android Parcelable 序列化复杂数据结构
参考博文 http://blog.csdn.net/yangzl2008/article/details/7593226 由于项目需要,Activity之间要传递一个特别复杂的数据结构对象,由于以前序 ...
- 如何在阿里云上运行SAP UI5应用
本来Jerry觉得这个知识点太简单了完全不值得写成微信公众号文章,但转念一想,可能网络上有一些刚刚初学UI5的朋友们可能会问到,所以还是写了. 今天一个成都同事问我这个问题,因为SAP WebIDE可 ...
- c#通用语言运行时CLR
- Git学习笔记08-远程仓库
因为想在家里和公司都能用到一套代码,所以选择上传到github,记录一下使用经验. 需要安装git,和注册github 以下操作是第一次将自己的代码上传到GitHub上 1)创建github项目 1. ...