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
<br />Original text: http://blog.rexsong.com...
If you’re looking for inspiration for columnar web...
Demand: This demand is an urgent need! In a subwa...
1. this.$router.push() 1. Vue <template> &l...
1. Download and install VirtualBox software First...
What if you designers want to use the font below ...
The replace statement is generally similar to ins...
Table of contents 1. Class 1.1 constructor() 1.2 ...
Table of contents 1. We must ensure that the vue/...
background Two network cards are configured for t...
!DOCTYPE Specifies the Document Type Definition (...
When writing a Dockerfile, include an entrypoint ...
On the road to self-learning game development, th...
Preface Recently, I added two fields to a table i...
1. Docker installation and startup yum install ep...