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]来实现,传递到后端的数据是以二进制的格式传递,所以上传图片的请求与普通的请求稍微 ...
随机推荐
- 写给Web开发人员看的Nginx介绍
译者注:不知道其他开发者是否和我一样,参与或者写了很多Web项目,但是却没有真正的去完整的部署应用,很多时候都是交给ops即运维的同学帮忙来做.而作为一个有节操的开发者,我认为了解一些服务器方面的知识 ...
- [转载]三十分钟理解:线性插值,双线性插值Bilinear Interpolation算法
[转载]三十分钟理解:线性插值,双线性插值Bilinear Interpolation算法 来源:https://blog.csdn.net/xbinworld/article/details/656 ...
- PHP常见函数
有时候,运行nginx和PHP CGI(PHP FPM)web服务的Linux服务器,突然系统负载上升,用top命令查看,很多phpcgi进程的CPU利用率接近100%后来通过跟踪发现,这种情况与PH ...
- Layui 实现input 输入和选择
<div class="layui-col-md4"> <label class="layui-form-label">移交单位< ...
- Java 面向对象(三)static 关键字
一.static 1.概述 static 的意思的静态的,也是一种修饰符. 关于 static 关键字的使用,它可以用来修饰的成员变量和成员方法,被修饰的成员是属于类的,而不是单单属于某个对象的. 用 ...
- 2019年6月SAP发布的未来ABAP平台的发展方向
未来ABAP平台将始终是这些产品的技术平台: S/4HANA On-Premises和Cloud将基于一个统一的ABAP codeline: SAP云平台上的ABAP编程环境: 什么是SAP Clou ...
- 使用url_for()时,会自动调用转换器的to_url()方法
视图反推url,在动态url(转换器)反推中的应用 # -*- coding: utf-8 -*- from flask import Flask, url_for, redirect from we ...
- CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
python2.7 yum install -y zlib zlib-devel openssl openssl-devel mysql-devel gcc gcc-c++ wget https:// ...
- webstorm 2018.2.5最新激活方式
亲测时间 2019年6月27日 08:45:52 下载破解文件: https://pan.baidu.com/s/1CMh5AYgewZflMVWL9BO-hA 打开webstorm / bi ...
- Pandas-Numpy-Scipy-Matplotlib
Ref:Using iloc, loc, & ix to select rows and columns in Pandas DataFrames 单纯的方括号只有两个用途:基于列的label ...