How to upload the WeChat Mini Programs page form to the server together with the picture

Gods; help solve it.


take it apart and write.
form submission is wx.request
upload picture is wx.uploadFile

you need to write a general image upload interface, and the url of the picture is returned by the background of uploading the image. This general interface can be used in any form that needs to submit a picture.

when you add a picture, call the image upload API to get the url, of the picture and store it in the form data. It would be nice to submit the form in the same way as the normal form.

export function uploadFiles(filePath,token){   // 
  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url:`${URL_PREV}upload`,
      filePath:filePath,
      name: 'file',
      header:{
        'content-type':'multipart/form-data',
        'x-token':token
      },
      success: function(res){
        let data = JSON.parse(res.data);
        if (data.code==200) {
          resolve(data.link);
        }else {
          reject(data.message);
        }
      }
    })
  }).catch(function(e){
    wx.showToast({
       title: e,
       icon: 'none',
       duration: 1500
    })
  });
};

adding an image is an interface. Get the url, of the picture and submit it with form.


upload by uploadFile and then return the file name and save it. The request submission form is submitted together in data {}


https://developers.weixin.qq.
to read the document! Look at formData

Menu