The scene that

Wechat mini program – upload pictures (multiple selection of pictures, image enlargement and preview)

Contrib.gie10. IO/appellate-appellate /…

1. Importing components

<config>
{
  navigationBarTitleText: 'xxxx',
  usingComponents: {
    "van-uploader": "module:@vant/weapp/dist/uploader",
  }
}
</config>
Copy the code

2. Code modules

<van-uploader
  preview-size="72"
  multiple
  data-idx="{{index}}"// Pass in the binding value as required by the businessfile-list="{{ item.fileList }}"// Select the image file arraymax-count="9"// Select the maximum number of imagesbind:after-read="afterRead"// Select the image callbackbind:delete="deleteClick"// Image deletion event />
Copy the code
// data
data: {
  stairs_Items: [].// Array of images
},

// Select the image callback
afterRead(event) {
  let that = this;
  wx.showLoading({
    title: '上传中...'
  });
  const { file } = event.$wx.detail;
  let uploadPromiseTask = [];
  for (let i = 0; i < file.length; i++) {
    uploadPromiseTask.push(this.uploadFile(file[i].url));
  }
  Promise.all(uploadPromiseTask)
    .then(res= > {
      that.stairs_Items[
        event.$wx.currentTarget.dataset.idx
      ].fileList = that.stairs_Items[
        event.$wx.currentTarget.dataset.idx
      ].fileList.concat(res);
      wx.hideLoading();
    })
    .catch(error= > {
      wx.hideLoading();
      wx.showToast({
        title: 'Upload failed! '.icon: 'none'
      });
    });
},
      
// Delete the image of the specified location
deleteClick(event) {
  if (this.stairs_Items) {
    const { index } = event.$wx.detail;
    this.stairs_Items[event.$wx.currentTarget.dataset.idx].fileList.splice(
      index,
      1); }},// Image upload
uploadFile(uploadFile) {
  return new Promise((resolve, reject) = > {
    wx.uploadFile({
      url: utils.imageUri + 'resource/resources/upload'.// Upload the image address
      filePath: uploadFile,   // Upload the file
      name: 'file'.success: res= > {
        // Upload complete
        const data = JSON.parse(res.data);
        const fileUrl = utils.imageUri + data.data.fileUrl;
        resolve({ url: fileUrl });
      },
      fail: err= >{ reject(err); }}); }); },Copy the code

Props (parameters)

parameter instructions type The default value
name Identifier, which can be obtained in the second argument to the callback function string | number
accept Acceptable file type. The optional value isall media image file video string image
sizeType The size of the selected picture whenacceptforimageType to set the size of the selected pictureoriginal compressed string[] ['original','compressed']
preview-size The size of the preview image and upload area. The default unit ispx string | number 80px
preview-image Whether to display the preview image after uploading boolean true
preview-full-image Whether to display a full-screen image preview after clicking the preview image boolean true
multiple Whether to enable image multiple selection, some Android models do not support boolean false
disabled Whether file uploading is disabled boolean false
show-upload Whether to display the file upload button boolean true
deletable Whether to display the delete button boolean true
capture Image or video selection mode, whenacceptforimageType-time settingcaptureOptional value iscameraYou can just turn on the camera string | string[] ['album', 'camera']
max-size File size limit, unit:byte number
max-count Maximum number of files to be uploaded number
upload-text Uploading area text prompt string
image-fit Preview image clipping mode, optional values refer to the appletimageThe component’smodeattribute string scaleToFill
use-before-read Whether to enable the event before file reading boolean
camera When the acceptvideoIs valid. The value isback front string
compressed When the acceptvideoThe default value is Whether to compress videostrue boolean
max-duration When the acceptvideoMaximum video shooting time (unit: second) number
upload-icon Upload area icon. For optional values, seeIcon component string plus

The legal value of Accept

parameter instructions
media Pictures and videos
image The picture
video video
file Select files other than pictures and videos from the client session
all Select all files from the client session

An Event

The event name instructions The callback parameter
bind:before-read Before the file is read, return in the callback functionfalseCan terminate file reading, binding events at the same time need to beuse-before-readProperty set totrue event.detail.file: The file currently read,event.detail.callback: callback function, callcallback(false)Abort file reading
bind:after-read After the file is read event.detail.file: File currently read
bind:oversize File size limit exceeded
bind:click-preview Click on the preview image event.detail.index: Click the serial number of the picture
bind:delete Delete the picture event.detail.index: Deletes the serial number of the image