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

The Complete Guide to Grid Layout in CSS

Grid is a two-dimensional grid layout system. Wit...

Sample code for implementing form validation with pure CSS

In our daily business, form validation is a very ...

Summary of MySQL stored procedure permission issues

MySQL stored procedures, yes, look like very rare...

Angular Dependency Injection Explained

Table of contents Overview 1. Dependency Injectio...

Pure CSS and Flutter realize breathing light effect respectively (example code)

Last time, a very studious fan asked if it was po...

Detailed Analysis of the Differences between break and last in Nginx

Let's talk about the difference first last, t...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...

Use Docker to create a distributed lnmp image

Table of contents 1. Docker distributed lnmp imag...

MySQL 5.6.24 (binary) automatic installation script under Linux

This article shares the mysql5.6.24 automatic ins...

Vue + element to dynamically display background data to options

need: Implement dynamic display of option values ...

HTML elements (tags) and their usage

a : Indicates the starting or destination positio...

mysql 5.7.20 win64 installation and configuration method

mysql-5.7.20-winx64.zipInstallation package witho...

MySQL 8.0.15 installation graphic tutorial and database basics

MySQL software installation and database basics a...

43 Web Design Mistakes Web Designers Should Watch Out For

This is an article about website usability. The a...