- Add a button and an image tag in the wxml file to display the uploaded image
<view> <button bindtap="chooseImage">选择图片</button> <image src="{ {imageUrl}}" /> </view>
- Add the method of selecting pictures and uploading pictures in the js file
Page({ data: { imageUrl: '' }, chooseImage() { wx.chooseImage({ count: 1, // 可选择的图片数量 sizeType: ['compressed'], // 压缩图片 sourceType: ['album', 'camera'], // 来源:相册或相机 success: (res)=> { // 将选择的图片上传到服务器 this.uploadImage(res.tempFilePaths[0]); } }) }, uploadImage(imagePath) { wx.uploadFile({ url: '服务器地址', // 上传图片的接口地址 filePath: imagePath, // 图片文件路径 name: 'image', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容 success: (res) => { // 上传成功后,将服务器返回的图片地址更新到image标签中 this.setData({ imageUrl: res.data }); }, fail: function (res) { console.log(res); } }) } })
WeChat applet realizes image upload (clear version)
Guess you like
Origin blog.csdn.net/qq_42044542/article/details/129666130
Recommended
Ranking