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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36b9a-4d6d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36b9a-4d6d5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?