How to use WeChat applet to upload pictures to the server

php中世界最好的语言
Release: 2018-04-14 16:42:25
Original
4017 people have browsed it

This time I will show you how the WeChat applet can upload picturesto the server. What are theprecautionsfor the WeChat applet to upload pictures to the server. The following is a practical case. Let’s take a look.

-wxml     /**选择图片 */ choose: function () { var that = this wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { var tempFilePaths = res.tempFilePaths that.setData({ tempFilePaths: res.tempFilePaths }) console.log(res.tempFilePaths) wx.setStorage({ key: "card", data: tempFilePaths[0] }) } }) },
Copy after login
2. Use wx.uploadFile to upload the image just uploaded to the server

formSubmit2: function (e) { var that = this var card = wx.getStorageSync('card') wx.uploadFile({ url: app.globalData.create_funds, filePath: card, name: 'card', formData: { 'user_id': app.globalData.user_id, 'person': e.detail.value.person, 'company': e.detail.value.company, }, success: function (res) { console.log(res) } }) } } },
Copy after login

PS: WeChat applet uploads one or more pictures

1. Key points

1.Select picture

wx.chooseImage({ sizeType: [], // original 原图,compressed 压缩图,默认二者都有 sourceType: [], // album 从相册选图,camera 使用相机,默认二者都有 success: function (res) { console.log(res); var array = res.tempFilePaths, //图片的本地文件路径列表 } })
Copy after login

2.Upload pictures

wx.uploadFile({ url: '', //开发者服务器的 url filePath: '', // 要上传文件资源的路径 String类型!!! name: 'uploadFile', // 文件对应的 key ,(后台接口规定的关于图片的请求参数) header: { 'content-type': 'multipart/form-data' }, // 设置请求的 header formData: { }, // HTTP 请求中其他额外的参数 success: function (res) { }, fail: function (res) { } })
Copy after login

2. Code examples

// 点击上传图片 upShopLogo: function () { var that = this; wx.showActionSheet({ itemList: ['从相册中选择', '拍照'], itemColor: "#f7982a", success: function (res) { if (!res.cancel) { if (res.tapIndex == 0) { that.chooseWxImageShop('album') } else if (res.tapIndex == 1) { that.chooseWxImageShop('camera') } } } }) }, chooseWxImageShop: function (type) { var that = this; wx.chooseImage({ sizeType: ['original', 'compressed'], sourceType: [type], success: function (res) { /*上传单张 that.data.orderDetail.shopImage = res.tempFilePaths[0], that.upload_file(API_URL + 'shop/shopIcon', res.tempFilePaths[0]) */ /*上传多张(遍历数组,一次传一张) for (var index in res.tempFilePaths) { that.upload_file(API_URL + 'shop/shopImage', res.tempFilePaths[index]) } */ } }) }, upload_file: function (url, filePath) { var that = this; wx.uploadFile({ url: url, filePath: filePath, name: 'uploadFile', header: { 'content-type': 'multipart/form-data' }, // 设置请求的 header formData: { 'shopId': wx.getStorageSync('shopId') }, // HTTP 请求中其他额外的 form data success: function (res) { wx.showToast({ title: "图片修改成功", icon: 'success', duration: 700 }) }, fail: function (res) { } }) },
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of using template strings in ES6

How to operate tables in Bootstrap

The above is the detailed content of How to use WeChat applet to upload pictures to the server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!