微信小程序实现上传图片功能
作者:李无敌 时间:2024-05-02 17:29:03
微信小程序图片上传,供大家参考,具体内容如下
先来看一下微信小程序的api
来看一下页面效果
查看大图
wxml文件代码:
<view class="weui-cell">
<view class="weui-cell__bd">
<view class="weui-uploader">
<view class="weui-uploader__hd">
<view class="weui-uploader__title">营业执照</view>
<view class="weui-uploader__info">{{imageList.length}}/{{count[countIndex]}}</view>
</view>
<view class="weui-uploader__bd">
<view class="weui-uploader__files">
<block wx:for="{{imageList}}" wx:for-item="image">
<view class="weui-uploader__file">
<image class="weui-uploader__img" src="{{image}}" data-src="{{image}}" bindtap="previewImage"></image>
</view>
</block>
</view>
<view class="weui-uploader__input-box">
<view class="weui-uploader__input" bindtap="chooseImage"></view>
</view>
</view>
</view>
</view>
</view>
js文件代码
chooseImage: function () {
var that = this;
console.log('aaaaaaaaaaaaaaaaaaaa')
wx.chooseImage({
count: this.data.count[this.data.countIndex],
success: function (res) {
console.log('ssssssssssssssssssssssssss')
//缓存下
wx.showToast({
title: '正在上传...',
icon: 'loading',
mask: true,
duration: 2000,
success: function (ress) {
console.log('成功加载动画');
}
})
console.log(res)
that.setData({
imageList: res.tempFilePaths
})
//获取第一张图片地址
var filep = res.tempFilePaths[0]
//向服务器端上传图片
// getApp().data.servsers,这是在app.js文件里定义的后端服务器地址
wx.uploadFile({
url: getApp().data.servsers + '/weixin/wx_upload.do',
filePath: filep,
name: 'file',
formData: {
'user': 'test'
},
success: function (res) {
console.log(res)
console.log(res.data)
var sss= JSON.parse(res.data)
var dizhi = sss.dizhi;
//输出图片地址
console.log(dizhi);
that.setData({
"dizhi": dizhi
})
//do something
}, fail: function (err) {
console.log(err)
}
});
}
})
},
previewImage: function (e) {
var current = e.target.dataset.src
wx.previewImage({
current: current,
urls: this.data.imageList
})
}
java 后端代码:
//获取当前日期时间的string类型用于文件名防重复
public String dates(){
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}
@RequestMapping("wx_upload.do")
public void uploadPicture(HttpServletRequest request, HttpServletResponse response,PrintWriter writer) throws Exception {
System.out.println("进入get方法!");
//获取从前台传过来得图片
MultipartHttpServletRequest req =(MultipartHttpServletRequest)request;
MultipartFile multipartFile = req.getFile("file");
//获取图片的文件类型
String houzhu=multipartFile.getContentType();
int one = houzhu.lastIndexOf("/");
System.out.println(houzhu.substring((one+1),houzhu.length()));
System.out.println(multipartFile.getName());
//根据获取到的文件类型截取出图片后缀
String type=houzhu.substring((one+1),houzhu.length());
System.out.println(multipartFile.getContentType());
String filename;
// request.getRealPath获取我们项目的根地址在加上我们要保存的地址
String realPath = request.getRealPath("/upload/wximg/");
try {
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdir();
}
//获取到当前的日期时间用户生成文件名防止文件名重复
String filedata=this.dates();
//生成一个随机数来防止文件名重复
int x=(int)(Math.random()*1000);
filename="zhongshang"+x+filedata;
System.out.println(x);
将文件的地址和生成的文件名拼在一起
File file = new File(realPath,filename+"."+type);
multipartFile.transferTo(file);
//将图片在项目中的地址和isok状态储存为json格式返回给前台,由于公司项目中没有fastjson只能用这个
JSONObject jsonObject=new JSONObject();
jsonObject.put("isok",1);
jsonObject.put("dizhi","/upload/wximg/"+filename+"."+type);
writer.write(jsonObject.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
来看一下之前在前端js输出的内容:
打开浏览器用我们的服务器的地址加上后台返回json的dizhi字段去访问这张图片
我们可以看到图片已经填入我们的服务器端里了,然后在打开我们服务器端项目根地址下面的/upload/wximg
到这里就大功告成了如果是多张图片上传可以在js里面根据要上传的数量循环上传。
为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。
来源:https://blog.csdn.net/qq_35342288/article/details/80434573
标签:微信小程序,上传图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python简单实现9宫格图片实例
2021-10-22 13:23:25
![](https://img.aspxhome.com/file/2023/6/81726_0s.png)
分享PHP header函数使用教程
2023-09-04 12:07:14
详解查看Python解释器路径的两种方式
2022-03-10 06:49:24
各种鼠标经过图片边框加粗效果整理
2007-09-29 21:43:00
OAuth从1.0到2.1的发展之路
2023-09-25 22:30:07
![](https://img.aspxhome.com/file/2023/4/112054_0s.jpg)
源码分析系列之json_encode()如何转化一个对象
2023-07-15 05:25:54
19个MySQL性能优化要点解析
2024-01-18 16:03:14
python中关于时间和日期函数的常用计算总结(time和datatime)
2022-01-02 05:50:08
pandas 实现某一列分组,其他列合并成list
2022-06-12 13:49:21
![](https://img.aspxhome.com/file/2023/8/95578_0s.jpg)
Python实现随机生成算术题的示例代码
2022-03-30 09:49:38
![](https://img.aspxhome.com/file/2023/3/114533_0s.jpg)
php之app消息推送案例教程
2023-06-15 00:42:02
![](https://img.aspxhome.com/file/2023/6/55556_0s.png)
python PyTorch参数初始化和Finetune
2023-04-26 08:53:44
![](https://img.aspxhome.com/file/2023/4/75934_0s.png)
一个挺酷的星级投票效果
2010-08-03 12:44:00
![](https://img.aspxhome.com/file/UploadPic/20108/3/star-63s.png)
ul设置浮动后不能自适应高度
2011-01-25 12:48:00
利用git克隆历史版本(下载指定版本的代码)
2022-06-14 11:03:17
![](https://img.aspxhome.com/file/2023/9/132299_0s.png)
mysql中数据库覆盖导入的几种方式总结
2024-01-19 22:26:33
![](https://img.aspxhome.com/file/2023/1/103131_0s.png)
Python 读取图片文件为矩阵和保存矩阵为图片的方法
2022-07-23 05:21:11
Python中循环后使用list.append()数据被覆盖问题的解决
2023-02-08 20:45:49
针对google Chrome的 CSS hacks
2009-11-30 12:45:00
快速掌握如何使用SQL Server来过滤数据
2009-01-15 13:27:00