asp.net upload Image Validation Code Small example _ Practical tips

Source: Internet
Author: User
First, client-side mode validation
Copy Code code as follows:

<script language= "JavaScript" >
Img=new Image ();
function Check_filetypeandfilesize ()
{
Get Upload Path
var Str=document.all.uploadfile.value;
Verify that the upload path is not empty
if (str== "")
{
Alert ("Please select the picture you want to upload first!") ");
return false;
}
Verify that the upload file is in the correct format
var pos = Str.lastindexof (".");
var LastName = str.substring (pos,str.length)
if (Lastname.tolowercase ()!= ". jpg" && lastname.tolowercase ()!= ". gif")
{
Alert ("You upload a file type of" LastName ", the picture must be JPG, GIF type");
return false;
}
Verify that the upload file has a wide and high ratio
if (img.width/img.height>1.6)
{
return confirm (you upload more than 8:5 of the picture, confirm to upload it?) );
}
Verify that the upload file is out of size
if (img.filesize/1024>150)
{
Alert ("The size of the file you uploaded exceeds the 150K limit!") ");
return false;
}
return true;
}
Upload file Box Address Change event, instant preview picture
function Changephoto ()
{
Img.src=document.all.uploadfile.value;
}
-->
</script>
<div>
<upload:inputfile id= "UploadFile" name= "Uploadfilepath" runat= "Server" onchange= "Changephoto ()"/>
<asp:button id= "Btnupload" runat= "server" text= "upload" onclientclick= "return Check_filetypeandfilesize ()"
onclick= "Btnupload_click"/>
</div>
<div class= "Upload" >

</div>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.