Vant Uploader implements the component of uploading one or more pictures

Vant Uploader implements the component of uploading one or more pictures

This article shares the Vant Uploader component for uploading one or more pictures for your reference. The specific content is as follows

HTML part

<template>
  <div class="contWrap">
    <van-uploader
      v-model="fileList"
      :multiple="true"
      :before-read="beforeRead"
      :after-read="afterRead"
      :before-delete="delUploadImg"
      upload-icon="plus"
    >
    <!-- Note: This is a custom upload style-->
      <!-- <p>
        <van-icon
          name="plus"
          color="#07c160"
          size=".5rem"
        />
        Upload photo</p> -->
    </van-uploader>
  </div>
</template>

js part

<script>
import axios from "axios";
export default {
  name: "uploadImages",
  data() {
    return {
      fileList: [],
      uploadUrl: "/api/upload/fileUpload",
      headers: {
        token: this.$store.state.account.token,
      },
    };
  },

  methods: {
    // Return Boolean value beforeRead(file) {
      if (file instanceof Array) {
        //Judge whether it is an array file.forEach((v) => {
          this.checkFile(v);
        });
      } else {
        this.checkFile(file);
      }
      return true;
    },
    //Image type checkcheckFile(file) {
      const format = ["jpg", "png", "jpeg"];
      const index = file.name.lastIndexOf(".");
      const finalName = file.name.substr(index + 1);
      if (!format.includes(finalName.toLowerCase())) {
        Toast("Please upload" + format.join(",") + "Format picture");
        return false;
      }
    },
    afterRead(file) {
    // You can upload the file to the server by yourself at this time if (file instanceof Array) {
        file.map((v) => {
          v.status = "uploading";
          v.message = "Uploading...";
          this.uploadImg(v);
        });
      } else {
        file.status = "uploading";
        file.message = "Uploading...";
        this.uploadImg(file);
      }
    },
    //Upload uploadImg(file) {
      const formData = new FormData();
      formData.append("file", file.file);
      axios
        .post(this.uploadUrl, formData, {
          headers: this.headers,
        })
        .then((res) => {
          if (res.data) {
            if (file instanceof Array) {
              //Judge whether it is an array file.map((v, i) => {
                v.status = "success";
                v.message = "";
                v.url = res.data[i];
              });
            } else {
              file.status = "success";
              file.message = "";
              file.url = res.data.uploadUrl;
            }
          }
        })
        .catch((err) => {
          this.$notify({
            type: "warning",
            message: "Upload failed",
          });
        });
    },
    //delete delUploadImg(item) {
      this.fileList = this.fileList.filter((v) => v.url != item.url);
    }
  },
};
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • The Uploader of Vue+Vant on the mobile terminal realizes the functions of uploading, compressing and rotating pictures
  • Vant uploader implements the drag-and-drop function for uploading pictures (set as cover)

<<:  How to create a view in MySQL

>>:  Steps for restoring a single MySQL table

Recommend

Implementation steps for installing FTP server in Ubuntu 14.04

Table of contents Install Software Management Ano...

How to install vim editor in Linux (Ubuntu 18.04)

You can go to the Ubuntu official website to down...

Dissecting the advantages of class over id when annotating HTML elements

There are very complex HTML structures in web pag...

Implementation of HTML sliding floating ball menu effect

CSS Styles html,body{ width: 100%; height: 100%; ...

Detailed explanation of the API in Vue.js that is easy to overlook

Table of contents nextTick v-model syntax sugar ....

Solution to VMware virtual machine no network

Table of contents 1. Problem Description 2. Probl...

Summary of WEBAPP development skills (notes for mobile website development)

1. To develop web responsively, the page must ada...

Detailed explanation of the practical application of centos7 esxi6.7 template

1. Create a centos7.6 system and optimize the sys...

Detailed explanation of Nginx's rewrite module

The rewrite module is the ngx_http_rewrite_module...

How to use default values ​​for variables in SASS

Variables defined in SASS, the value set later wi...

How to implement the @person function through Vue

This article uses vue, and adds mouse click event...

Docker image import and export code examples

Import and export of Docker images This article i...