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

Detailed explanation of CSS pre-compiled languages ​​and their differences

1. What is As a markup language, CSS has a relati...

Docker-compose tutorial installation and quick start

Table of contents 1. Introduction to Compose 2. C...

CSS achieves colorful and smart shadow effects

background Ever wondered how to create a shadow e...

The difference and choice between datetime and timestamp in MySQL

Table of contents 1 Difference 1.1 Space Occupanc...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

How to fix the footer at the bottom of the page (multiple methods)

As a front-end Web engineer, you must have encoun...

Detailed steps to install web server using Apache httpd2.4.37 on centos8

Step 1: yum install httpd -y #Install httpd servi...

A brief discussion on Mysql specified order sorting query

Recently, I have been working on a large-screen d...

Prototype and prototype chain prototype and proto details

Table of contents 1. Prototype 2. Prototype chain...

WeChat applet implements the Record function

This article shares the specific code for the WeC...

A brief analysis of SQL examples for finding uncommitted transactions in MySQL

A long time ago, I summarized a blog post titled ...

Summary of @ usage in CSS (with examples and explanations)

An at-rule is a declaration that provides instruc...

Share 13 basic syntax of Typescript

Table of contents 1. What is Ts 2. Basic Grammar ...

Share 9 Linux Shell Scripting Tips for Practice and Interviews

Precautions 1) Add interpreter at the beginning: ...