Micro-channel cloud development of small programs upload pictures wx.cloud.uploadFile ()

Page button

<button bindtap="addImg" class="addPng" type="default">添加图片</button>

js: Before using the picture certainly has to choose to upload pictures, so before you want to use wx.chooseImage () Select the picture, and then upload pictures

addImg : function(){

  wx.chooseImage ({// Select Image

      count: 1, // a predetermined number of images selected by default 9

      sizeType: [ "original", "compressed"], // specified picture size, picture / map compression

      sourceType: [ 'album', 'camera'], // where to select the picture album / camera

      Functions performed (chooseres) => {// interface call when success: success

          //console.log(chooseres);

          // Select the picture you can upload it here

          wx.cloud.uploadFile({

            cloudPath: "img /" + new Date () getTime () + "-" + Math.floor (Math.random () * 1000), // cloud storage path and file name.

            filePath: chooseres.tempFilePaths [0], is to choose a temporary address returned Pictures // image to upload / file path used here

            success: (uploadres) => {// upload pictures to the success of cloud storage

              //console.log(uploadres)

              wx.showLoading ({// display a loading message box does not automatically shut down only wx.hideLoading Close

                title: "Uploading Image", // prompt box displays the message

                mask: true, // show transparency mask layer, preventing touch. Can operate the screen can not be operated on the screen when prompted to true, or not write is false

                success : function () {

                  setTimeout (function () {// use timer allows prompt box disappears

                    wx.hideLoding () // make prompt box hide, disappear

                  }, 1000); // timer time (ms) after the execution shown here 1s message box displayed wx.hideLoding (), i.e., a message box displayed for one second

                }

              });

            },

            fail : (err) => {

              console.log(err)

            }

          })

      },

      fail : (err) => {

        console.log(err)

      }

  })

}

success:(chooseres):

The above code chooseres return value success has three parameters, a message is returned to the first, the second picture is a temporary address list (array), regardless of the selected number is 1 or other, is an array, so take when the value of a particular location are required to get the subscripts;image

The third document is an array of objects, where each position is stored temporarily address the size of the size and path of the file

image

cloudPath: "img/" + new Date().getTime() +"-"+ Math.floor(Math.random() * 1000):

The path to the folder "/" if the division "/" in front of the folder is not created automatically "/" followed by the file name;

image

success: (uploadres) returns a value in the message, fileID (file access paths), and HTTP status code

But the choice is not the same and that each upload can only upload a picture, so every time returned fileID access path to the file is a single access path corresponding picture

image

So far the development of cloud picture applet upload is complete

Guess you like

Origin www.cnblogs.com/Guo-Xiaoqiang/p/12114826.html