项目中肯定会遇到上传文件到服务器端,小程序提供了很有用的api
wxml代码:
复制代码
1
2<image mode='widthFix' src="{{imgUrl}}"></image> <view bindtap="getPhoto">上传图片</view>
js代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41getPhoto(e){ //获取手机相册功能 let _this = this; wx.chooseImage({ count: 1, //上传图片数量 sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { console.log(res) // tempFilePath可以作为img标签的src属性显示图片 const tempFilePaths = res.tempFilePaths _this.upLoad(_this, tempFilePaths); //调用上传接口 } }) }, upLoad(that,filePath){ wx.uploadFile({ url: ROOT_URL, //换成你自己的url地址 filePath: filePath[0], //图片文件 name: 'file', header: { "Content-Type": "multipart/form-data", //头部设置 }, formData: { //这里放你自己额外要带的参数 }, success: function (res) { //console.log(res) var data = JSON.parse(res.data) //do something //根据你自己的项目要求,做处理 this.setData({ imgUrl:filePath[0] }) } }) },
最后
以上就是忧伤草丛最近收集整理的关于微信小程序上传图片到服务器wx.uploadFile的全部内容,更多相关微信小程序上传图片到服务器wx内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复