Project scenario:1. Upload file restrictions Function: 1. Prevent front-end operations from uploading abnormal files Functionality: 1. Get the file instance The code is as follows: //Size limit checkFileSize(file, rules) { return new Promise((resolve, reject) => { file.size / 1024 / 1024 > rules ? reject() : resolve() }).then( () => { return true }, () => { //Operation prompt return Promise.reject() } ) }, //Upload format restrictions checkFileType(file, rules) { return new Promise((resolve, reject) => { rules && rules.includes(file.type) ? resolve() : reject() }).then( () => { return true }, () => { //Operation prompt return Promise.reject() } ) }, //Aspect ratio (image) checkImageWH(file, rules) { const _this = this return new Promise((resolve, reject) => { //Read file const filereader = new FileReader() filereader.readAsDataURL(file) filereader.onload = e => { const src = e.target.result const image = new Image() image.onload = function() { //Analyze the data and determine whether it complies with the rules resolve() } image.onerror = reject image.src = src } }).then( () => { return true }, () => { //Operation prompt return Promise.reject() } ) }, //Aspect ratio (video) checkVideoWH(file, rules) { return new Promise(function(resolve, reject) { var url = URL.createObjectURL(file) var video = document.createElement('video') video.onloadedmetadata = evt => { URL.revokeObjectURL(url) //Analyze the data and determine whether it complies with the rules resolve() } video.src = url video.load() // fetches metadata }).then( () => { return true }, () => { //Operation prompt return Promise.reject() } ) } Actual call //Get the file instance Screen(){ //Get permission rules const { filesSize, filesFormat, fileLimit} = this // File size, file type, image/video width and height limits //Parameter judgment const isFileSize = filesSize ? await this.checkFileSize(file, filesSize) : true const isFileType = filesFormat ? await this.checkFileType(file, filesFormat) : true //Picture materialif (fileLimit && fileLimit.type * 1 === 1) { const isImageLimit = fileLimit? await this.checkImageWH(file, fileLimit) : true //output result return isFileSize && isFileType && isImageLimit } else if (fileLimit&& fileLimit.type * 1 === 2) { //Video material const isVideoLimit = fileLimit? await this.checkVideoWH(file, fileLimit) : true //output result return isFileSize && isFileType && isVideoLimit } else { //No limit //Output result return isFileSize && isFileType } } Summary: 1. Get an instance This is the end of this article about the detailed case of JavaScript upload file restriction parameter. For more relevant js upload file restriction parameter content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The MySQL server is running with the --read-only option so it cannot execute this statement
>>: How to Install Xrdp Server (Remote Desktop) on Ubuntu 20.04
The purpose of using cache is to reduce the press...
I saw in the LOFTER competition that it was mentio...
1. Basic grammar Copy code The code is as follows...
The table structure is as follows. There are only...
Navigation and other things are often used in dai...
There is no need to say much about the difference...
I would like to ask a question. In Dreamweaver, I...
The use of vue3 Teleport instant movement functio...
As the title says, otherwise when the page is revi...
Table of contents Background Configuring DHCP Edi...
Table of contents Cause of the problem: Solution:...
The /etc/network/interfaces file in Linux is used...
Enough of small talk <br />Based on the lar...
Because I have been tinkering with Linux recently...
Mainly for low version browsers <!-- --> is ...