Solution to Element-ui upload file upload restriction

Solution to Element-ui upload file upload restriction

question

Adding the type of uploaded file in accept only plays a "superficial" role. After selecting "all files", you can still upload any type of file, which has no restrictive effect at all.

Solution

Make a judgment in the before-upload hook (the hook before uploading files, the parameter is the uploaded file, if it returns false or returns a Promise and is rejected, the uploading will be stopped.) There is a pitfall here. When you set :auto-upload="false", this hook will not be triggered, so you can also make a judgment in on-change.

On the code

// :before-upload or :on-change binding method beforeUpload(file, fileList) {
   if (file.size / (1024 * 1024) > 500) { // Limit file size this.$message.warning(`The current limit file size cannot be greater than 500M`)
     return false
   }
   
   let suffix = this.getFileType(file.name) //Get the file suffix let suffixArray = ['jpg', 'png', 'jpeg', 'gif'] //Limited file types, define them according to the situation if (suffixArray.indexOf(suffix) === -1) {
     this.$message({
       message: 'File format error',
       type: 'error',
       duration: 2000
     })
   }
   return suffixArray
 },
 getFileType(name) {
   let startIndex = name.lastIndexOf('.')
   if (startIndex !== -1) {
     return name.slice(startIndex + 1).toLowerCase()
   } else {
     return ''
   }
 }

This is the end of this article about the solution to Element-ui upload file restrictions. For more information about Element upload restrictions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of uploading multiple files at once with el-upload in element-ui
  • Element-ui file upload method to modify the file name example
  • Detailed explanation of the front-end processing of uploaded files in Element-UI
  • Element-ui hides the upload button function after uploading a picture
  • Element-ui implementation example of multiple file upload
  • Use of vue-cli3.0+element-ui upload component el-upload
  • element-ui Mark the coordinate points after uploading the picture

<<:  Hadoop 2.x vs 3.x 22-point comparison, Hadoop 3.x improvements over 2.x

>>:  Detailed tutorial on installation and configuration of MySql 5.7.17 winx64

Recommend

Front-end advanced teaching you to use javascript storage function

Table of contents Preface Background Implementati...

MySQL 8.0.11 installation tutorial with pictures and text

There are many tutorials on the Internet, and the...

Two ways to implement text stroke in CSS3 (summary)

question Recently I encountered a requirement to ...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

Nginx solves cross-domain issues and embeds third-party pages

Table of contents Preface difficulty Cross-domain...

Vue+Element UI realizes the encapsulation of drop-down menu

This article example shares the specific code of ...

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box mod...

How to allow remote connection in MySql

How to allow remote connection in MySql To achiev...

MySQL Learning: Three Paradigms for Beginners

Table of contents 1. Paradigm foundation 1.1 The ...

Native javascript+CSS to achieve the effect of carousel

This article uses javascript+CSS to implement the...

MySQL 5.7.18 release installation guide (including bin file version)

The installation process is basically the same as...

mysql 5.7.5 m15 winx64.zip installation tutorial

How to install and configure mysql-5.7.5-m15-winx...

Vue3 Documentation Quick Start

Table of contents 1. Setup 1. The first parameter...

The meaning of the 5 types of spaces in HTML

HTML provides five space entities with different ...