Home Web Front-end JS Tutorial Uploaded js verification (image/file extension)_javascript skills

Uploaded js verification (image/file extension)_javascript skills

May 16, 2016 pm 05:35 PM
upload extension name

js verification uploaded image
Copy code The code is as follows:

var ImgObj =new Image();//Create an image object
varAllImgExt=".jpg|.jpeg|.gif|.bmp|.png|"//All image format types
varFileObj,ImgFileSize,ImgWidth,ImgHeight ,FileExt,ErrMsg,FileMsg,IsImg//Global variable image related attributes
//The following are restriction variables
var AllowExt=""; //The file types allowed to be uploaded are unlimited. Each extension must be followed by Add a "|" lowercase letter to represent
var AllowImgFileSize=100; //The size of the image file allowed to be uploaded 0 is unlimited Unit: KB
var AllowImgWidth=385; //The width of the image file allowed to be uploaded Ɓ is none Restriction unit: px (pixel)
var AllowImgHeight=441; //The height ƹ of the uploaded image is allowed to be unlimited. Unit: px (pixel)
function CheckProperty(obj) //Detect image properties
{
FileObj=obj;
if(ImgObj.readyState!="complete")//If the image is not loaded, perform loop detection
{
setTimeout("CheckProperty(FileObj)",500) ;
return false;
}
ImgFileSize=Math.round(ImgObj.fileSize/1024*100)/100;//Get the size of the image file
ImgWidth=ImgObj.width;//Get The width of the image
ImgHeight=ImgObj.height; //Get the height of the image
FileMsg="nImage size:" ImgWidth "*" ImgHeight "px";
FileMsg=FileMsg "nImage file size:" " ImgFileSize "Kb";
FileMsg=FileMsg "nImage file extension:" FileExt "nCan be uploaded! ";
ErrMsg="";
if(AllowImgWidth!=ImgWidth)
ErrMsg =ErrMsg "nPlease upload a file with a width equal to " AllowImgWidth "px. The current image width is " ImgWidth "px";
if(AllowImgHeight!=ImgHeight)
ErrMsg=ErrMsg "nPlease upload a file with a height equal to " AllowImgHeight " px file, the current image height is " ImgHeight "px";
if(AllowImgFileSize!=0&&AllowImgFileSizeErrMsg=ErrMsg "nPlease upload a file smaller than " AllowImgFileSize "KB, the current file size is " ImgFileSize "KB";
if(ErrMsg!="")
{
alert(ErrMsg);
return false;
}
else
return true;
}//end CheckProperty();
ImgObj.onerror=function(){ErrMsg='nThe image format is incorrect or the image is damaged!';}
function CheckExt(obj)
{
ErrMsg="";
FileMsg="";
IsImg=false;
if(obj.value=="")
return false;
FileExt=obj.value.substr( obj.value.lastIndexOf(".")).toLowerCase();
if(AllImgExt.indexOf(FileExt "|")!=-1)//If the image file, perform image information processing
{
IsImg=true;
FileObj=obj;
ImgObj.src=obj.value;
returnCheckProperty(obj);
}else
{
alert("This file Type not allowed to be uploaded. Please upload a file of type "AllImgExt", the current file type is "FileExt);
obj.value='';
return false;
}
}

Uploaded js verification
The following will introduce how to control the extension of an uploaded file
js:
Copy code<. 🎜> The code is as follows:
function check2()
{
var file = document.getElementsByName("file").value;
if( file=="")
{
alert("Please select a file");
return false;
}
var strTemp = file.split(".");
var strCheck = strTemp[strTemp.length-1];
if(strCheck.toUpperCase()=='JPG')
{
return true;
}else
{
alert('The uploaded file type is incorrect!');
return false;
}
}

Form:

Copy code The code is as follows:





It should be noted that document.getElementsByName("file").value obtains the absolute path of the uploaded file, so the string splitting method is used to split the extension of the file, and then the judgment is made.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

4 Quick Ways to Open Webp Images on Windows 11 4 Quick Ways to Open Webp Images on Windows 11 Sep 30, 2023 pm 02:13 PM

WebP or WebPicture is a modern image format developed by Google and has been widely used by the industry recently. These images are smaller than JPG, JPEG or PNG files, which increases page load speed and makes them valuable from an SEO perspective. But how to open WebP files in Windows 11? Given that it's a relatively new image format, first announced in March 2010, compatibility is bound to be an issue. Some photo editing software still don't support file extensions. But things change quickly! Does Windows 11 support WebP? Although WebP image files are not compatible with previous Windows 11, M

How to fix file names or extensions that are too long in Windows 11? How to fix file names or extensions that are too long in Windows 11? Apr 22, 2023 pm 04:37 PM

Have you ever faced any issues while transferring files that prevented you from doing so? Well, many Windows users have recently reported that they faced issues while copying and pasting files into a folder, where an error was thrown saying “The file name of the destination folder is too long”. Additionally, some other Windows users expressed frustration when opening any file and said "the file name or extension is too long" and they were unable to open the file. This disappoints users by not allowing them to transfer files to any other folder. While analyzing the issue, we have come up with a series of solutions that may help alleviate the issue and allow users to transfer files easily. If you are in a similar situation, please see this post for more information. Source: https

How to open files without extension in Windows 11, 10 How to open files without extension in Windows 11, 10 Apr 14, 2023 pm 05:52 PM

Having a file doesn't mean much unless you know its extension. Some files have no extension. Most of the time, the extension is probably just hidden and you can easily find it from Windows File Explorer itself using the methods listed in Section 1. Even if the file doesn't actually have an extension, it can be easily found by following the methods listed in Section 2. Finally, once you know the file extension, you can follow the steps in Section 3 to find an appropriate application to open your file. So, this article is a complete solution to all your file extension problems. Hope you enjoy reading! Section 1: How to Determine if a File Has an Extension The first step in opening a file without an extension is to make sure that the file actually does not have an extension. this

How to implement file upload and processing in FastAPI How to implement file upload and processing in FastAPI Jul 28, 2023 pm 03:01 PM

How to implement file upload and processing in FastAPI FastAPI is a modern, high-performance web framework that is easy to use and powerful. It provides native support for file upload and processing. In this article, we will learn how to implement file upload and processing functions in the FastAPI framework, and provide code examples to illustrate specific implementation steps. First, we need to import the required libraries and modules: fromfastapiimportFastAPI,UploadF

Simple steps to upload your own music on Kugou Simple steps to upload your own music on Kugou Mar 25, 2024 pm 10:56 PM

1. Open Kugou Music and click on your profile picture. 2. Click the settings icon in the upper right corner. 3. Click [Upload Music Works]. 4. Click [Upload Works]. 5. Select the song and click [Next]. 6. Finally, click [Upload].

How to upload lyrics to QQ Music How to upload lyrics to QQ Music Feb 23, 2024 pm 11:45 PM

With the advent of the digital age, music platforms have become one of the main ways for people to obtain music. However, sometimes when we listen to songs, we find that there are no lyrics, which is very disturbing. Many people hope that lyrics can be displayed when listening to songs to better understand the content and emotions of the songs. QQ Music, as one of the largest music platforms in China, also provides users with the function of uploading lyrics, so that users can better enjoy music and feel the connotation of the songs. The following will introduce how to upload lyrics on QQ Music. first

How to solve the problem of slow upload speed on Win10 computer How to solve the problem of slow upload speed on Win10 computer Jul 01, 2023 am 11:25 AM

How to solve the slow upload speed of Win10 computer? When we use the computer, we may feel that the file upload speed of our computer is very slow. So what is the situation? In fact, this is because the default upload speed of the computer is 20%, so the upload speed is very slow. Many friends do not know how to operate in detail. The editor has compiled the steps to format the C drive in Win11 below. If you are interested, follow Let’s take a look below! Solution to the slow upload speed of Win10 1. Press win+R to call up run, enter gpedit.msc, and press Enter. 2. Select the management template, click Network--Qos Packet Scheduler, and double-click Limit to reserve bandwidth. 3. Select Enabled, which will bring

How to deal with inconsistent file formats and extensions How to deal with inconsistent file formats and extensions Feb 19, 2024 pm 04:47 PM

How to solve the mismatch between file format and extension. During computer use, we often encounter mismatch between file format and extension. When we try to open a file, the system may prompt "The file cannot be opened" or "The file format is not supported." This situation can be confusing and frustrating, but it's actually not that difficult to fix and it only requires a few simple steps. First, we need to understand what the file format and extension mean. File format refers to the storage method and encoding method of the file. For example, a text file is

See all articles