微信小程序实现图片是上传、预览功能
本文实例讲述了微信小程序实现图片上传、删除和预览功能的方法,分享给大家供大家参考,具体如下:
这里主要介绍一下微信小程序的图片上传图片删除和图片预览


1、可以调用相机也可以从本地相册选择
2、本地实现微信小程序的上传照片、预览照片的功能
3、利用wx.chooseImage方法
4、附带了一些表单样式(可以忽略)
代码如下
wxml文件
<view class="numberInfo">
** 信息录入</view> <view class="container"> <view class="lineHeight" type="number">手机号
<input class='input' placeholder='请输入手机号'></input>
</view>
<view class="lineHeight" type="text">姓名
<input class='input-15' placeholder='姓名'></input>
</view>
<view class="lineHeight" type="text">公司名称
<input class='input-7' placeholder='公司名称'></input>
</view>
<view class="lineHeight">公司电话
<input class='input-7' type='number' placeholder='区号'></input>
</view>
<view class="lineHeight" type='number'>分机号码
<input class='input-7' placeholder='公司分机号码(选填)'></input>
</view>
<view class="lineHeight" type="text">
<!-- <input class='input-7'></input> -->
<picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}" bindtap='clearFont'>
产品/服务
<text class='select' >{{placeholder}} {{array[index]}}</text>
</picker>
</view>
<view class="lineHeight" type="text">
<!-- <input class='input-7' placeholder='请选择'></input> -->
<view class="section">
<!-- <view class="section__title">省市区选择器</view> -->
<picker
mode="region"
bindchange="bindRegionChange"
value="{{region}}"
custom-item="{{customItem}}"
>
<view class="picker">
公司地址 <text class='select'>{{region[0]}},{{region[1]}},{{region[2]}}</text>
</view>
</picker>
</view>
</view>
<view class="lineHeight" type="text">具体地址
<input class='input-7' placeholder='具体地址'></ input>
</view>
</view> <view class="weui-uploader">
<view class="img-v weui-uploader__bd">
<view class='pic' wx:for="{{imgs}}" wx:for-item="item" wx:key="*this">
<image class='weui-uploader__img '
src="{{item}}"
data-index="{{index}}" mode="aspectFill" bindtap="previewImg">
<icon type='cancel' class="delete-btn" data-index="{{index}}" catchtap="deleteImg"></icon>
</image>
</view> <!-- 用来提示用户上传图片 -->
<view class="weui-uploader__input-box pic" bindtap="chooseImg"> </view>
</view>
<button class="upload-img-btn" bindtap="chooseImg" type='primary'>拍照 / 上传</button>
</view>
css文件
/* pages/upload/upload.wxss */
.img{
display: inline-block;
} .pic {
float:left;
position:relative;
margin-right:9px;
margin-bottom:9px;
} .delete-btn{
position: absolute;
top: 0;
right: 0;
} .weui-uploader{
padding: 10rpx;
} .lineHeight {
width: 100%;
line-height: 80rpx;
border-bottom: 1px solid #ccc;
font-size: 30rpx;
}
.container {
padding: 0;
align-items: left;
padding-left: 15rpx;
}
.numberInfo {
font-size: 24rpx;
text-indent: 15rpx;
border-bottom: 1px solid #ccc;
} /* .input {
display: inline-block;
border: 1px solid #ccc;
line-height: 80rpx;
vertical-align: middle;
margin-left: 11%;
width: 75%;
} */
.input,
.input-7 ,
.input-15{
margin-left: 7%;
display: inline-block;
/* border: 1px solid #ccc; */
line-height: 80rpx;
vertical-align: middle;
width: 75%;
}
.input{
margin-left: 11%;
} button {
width: 100%;
margin-top: 30rpx;
}
.select{
margin-left: 7%;
color: #666;
} .input-15{
margin-left:15%;
}
js文件
// pages/upload/upload.js
Page({ /**
* 页面的初始数据
*/
data: {
imgs: [],
placeholder: '请选择',
array: ['发电机', '充电器', '引擎动力', '其他'],
objectArray: [
{
id: 0,
name: '发电机'
},
{
id: 1,
name: '充电器'
},
{
id: 2,
name: '引擎动力'
},
{
id: 3,
name: '其他'
}
], multiIndex: [0, 0, 0],
date: '2016-09-01',
time: '12:01',
region: ['广东省', '广州市', '海珠区'],
customItem: '全部'
},
// 上传图片
chooseImg: function (e) {
var that = this;
var imgs = this.data.imgs;
if (imgs.length >= 9) {
this.setData({
lenMore: 1
});
setTimeout(function () {
that.setData({
lenMore: 0
});
}, 2500);
return false;
}
wx.chooseImage({
// count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
var tempFilePaths = res.tempFilePaths;
var imgs = that.data.imgs;
// console.log(tempFilePaths + '----');
for (var i = 0; i < tempFilePaths.length; i++) {
if (imgs.length >= 9) {
that.setData({
imgs: imgs
});
return false;
} else {
imgs.push(tempFilePaths[i]);
}
}
// console.log(imgs);
that.setData({
imgs: imgs
});
}
});
},
// 删除图片
deleteImg: function (e) {
var imgs = this.data.imgs;
var index = e.currentTarget.dataset.index;
imgs.splice(index, 1);
this.setData({
imgs: imgs
});
},
// 预览图片
previewImg: function (e) {
//获取当前图片的下标
var index = e.currentTarget.dataset.index;
//所有图片
var imgs = this.data.imgs;
wx.previewImage({
//当前显示图片
current: imgs[index],
//所有图片
urls: imgs
})
}, bindPickerChange(e) {
console.log('picker发送选择改变,携带值为', e.detail.value)
this.setData({
index: e.detail.value
})
},
clearFont() {
this.setData({
placeholder: ''
})
}, bindRegionChange(e) {
console.log('picker发送选择改变,携带值为', e.detail.value)
this.setData({
region: e.detail.value
})
}, /**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () { }, /**
* 生命周期函数--监听页面显示
*/
onShow: function () { }, /**
* 生命周期函数--监听页面隐藏
*/
onHide: function () { }, /**
* 生命周期函数--监听页面卸载
*/
onUnload: function () { }, /**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () { }, /**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () { }, /**
* 用户点击右上角分享
*/
onShareAppMessage: function () { }
})
微信小程序实现图片是上传、预览功能的更多相关文章
- 微信小程序实现图片裁剪上传(wepy)
参考https://github.com/we-plugin/we-cropper,在wepy中实现,参考的具体例子是we-cropper/example/cutInside/ 项目上传图片时2:3的 ...
- 微信小程序 压缩图片并上传
转自https://segmentfault.com/q/1010000012507519 wxml写入 <view bindtap='uploadImg'>上传</view> ...
- 微信小程序裁剪图片后上传
上传图片的时候调起裁剪页面,裁剪后再回调完成上传; 图片裁剪直接用we-cropper https://github.com/we-plugin/we-cropper we-cropper使用详细 ...
- 微信小程序压缩图片并上传到服务器(拿去即用)
这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...
- 微信开发中使用微信JSSDK和使用URL.createObjectURL上传预览图片的不同处理对比
在做微信公众号或者企业微信开发业务应用的时候,我们常常会涉及到图片预览.上传等的处理,往往业务需求不止一张图片,因此相对来说,需要考虑的全面一些,用户还需要对图片进行预览和相应的处理,在开始的时候我使 ...
- 项目总结07:JS图片的上传预览和表单提交(FileReader()方法)
JS图片的上传预览和表单提交(FileReader()方法) 一开始没有搞明白下面这块代码的,今天有时间简单整理下 核心点:FileReader()方法 以下是代码(以JSP文件为例) <!DO ...
- JavaScript实现本地图片上传预览功能(兼容IE、chrome、FF)
需要解决的问题有:本地图片如何在上传前预览.编辑:最近发现这个功能很多是基于flash实现的,很多JavaScript实现的代码兼容性都很差,特别是在IE和firefox和chrome三个浏览器上不兼 ...
- JQ实现图片上传预览功能
<input type="file" name="img" id="test1"> <img src="&quo ...
- js实现本地的图片压缩上传预览
js在设计时考虑到安全的原因是不允许读写本地文件的,随着html5的出现提供了fileReader AP从而可以I实现本地图片的读取预览功能, 另外在移动端有的限制图片大小的需求,主要是考虑图片过大会 ...
随机推荐
- 15 个 Eclipse 常用开发快捷键使用技巧
15 个 Eclipse 常用开发快捷键使用技巧 1.alt+? 或 alt+/:自动补全代码或者提示代码 2.ctrl+o:快速outline视图 3.ctrl+shift+r:打开资源列表 4.c ...
- Spark2.0.0内存管理
来源:http://spark.apache.org/docs/2.0.0/configuration.html spark中的内存使用主要分为两类:执行和存储.执行内存指的是用于shuffles.j ...
- 啊 B树
关于B树的阶 B树的阶(英语对应order)定义是不统一的: Unfortunately, the literature on B-trees is not uniform in its termin ...
- 【GIT】git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚
[git 删除本地分支] git branch -D br [git 删除远程分支] git push origin :br (origin 后面有空格) git代码库回滚: 指的是将代码库某分支退 ...
- Linux下system()函数的实现
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types. ...
- C&C++ Calling Convention
tkorays(tkorays@hotmail.com) 调用约定(Calling Convention) 是计算机编程中一个比较底层的设计,它主要涉及: 函数参数通过寄存器传递还是栈? 函数参数从左 ...
- react-native中显示手机本地图片/视频
已知文件路径'/data/user/0/com.ycdj/files/media/218787782/efa1d12f22d2/1235.jpg' 只需在路径前面拼上file:///即可,如: < ...
- Windows本地代码仓库使用连接教程
目录 软件安装 修改语言为中文 克隆远程仓库 文件上传教程 软件安装 安装Git(软件下载链接) 根据自己的系统选择对应版本下载安装 安装TortoiseGit(软件下载链接) 1.下载完毕解压文件夹 ...
- Linux-1.Windows远程连接Linux的工具
1.下载工具 想要链接远程Linux服务器,就需要工具来进行连接. 工具一:连接远端Linux工具--putty(可以用xshell啥的,我懒,就弄了个这个,建议还是xshell哈,功能多,还好看) ...
- 百度网盘免VIP全速下载!
不知道大家在用百度网盘下载文件时会不会遇到这样一个问题: 过分! 太过分了! 100M的宽带你就给我限速到20KB/s... 当然 解决办法有很多 1.充钱(这辈子都不可能的) ······ 百度上有 ...