Home > WeChat Applet > Mini Program Development > How to upload pictures in WeChat applet development

How to upload pictures in WeChat applet development

php中世界最好的语言
Release: 2018-06-05 11:46:56
Original
2143 people have browsed it

The old rule is to go directly to the code.

<span style="font-size: 14px;">handleCancelPic() {        let id = this.data.dbId;<br/>        wx.chooseImage({<br/>          count: 3, // 默认9<br/>          sizeType: [&#39;compressed&#39;], // 可以指定是原图还是压缩图,默认二者都有          sourceType: [&#39;album&#39;, &#39;camera&#39;], // 可以指定来源是相册还是相机,默认二者都有<br/>          success: res => {<br/>            // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片<br/>            var tempFilePaths = res.tempFilePaths;<br/><br/>            this.setData({<br/>                src: tempFilePaths<br/>            })<br/>            upload(this,tempFilePaths,&#39;&#39;,&#39;&#39;);<br/>          }<br/>        })<br/>    }<br/>然后一个封装好的方法function upload(page, path,way,id) {<br/>    console.log(path)<br/>  wx.showToast({<br/>    icon: "loading",<br/>    title: "正在上传"<br/>  });<br/>  var test = [],<br/>    that = this;  for (var i = 0; i<path.length; i++) {<br/>        wx.uploadFile({<br/>          url: api.CancelImg,<br/>          filePath: path[i],          <br/>          name: &#39;file&#39;,<br/>          header: { "Content-Type": "multipart/form-data" },<br/>          success: res => {<br/>            test.push(res);<br/>            wx.setStorageSync(&#39;cancelImg&#39;,test)<br/>            console.log(test)            if (res.statusCode != 200) { <br/>              wx.showModal({<br/>                title: &#39;提示&#39;,<br/>                content: &#39;上传失败&#39;,<br/>                showCancel: false<br/>              })              return;<br/>            }else {<br/>                wx.showModal({<br/>                    title: &#39;提示&#39;,<br/>                    content: &#39;上传成功&#39;,<br/>                    showCancel: false<br/>                }) <br/>            }<br/>          },<br/>          fail: function (e) {<br/>            console.log(e);<br/>            wx.showModal({<br/>              title: &#39;提示&#39;,<br/>              content: &#39;上传失败&#39;,<br/>              showCancel: false<br/>            })<br/>          },<br/>          complete: function () {<br/>            wx.hideToast();  //隐藏Toast<br/>          }<br/>        })<br/>    }<br/></span>
Copy after login

This is a method for uploading multiple pictures. If you want to upload a single picture, just remove the loop. Mainly because WeChat’s official default is to upload one picture at a time, which is a pain in the ass. This is all we can do. . .

# 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:

The use of container views in WeChat applet development

How to handle Android request failure in WeChat applet development

The above is the detailed content of How to upload pictures in WeChat applet development. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template