Asp. Net upload image verification code small example
- 2020-06-15 08:06:19
- OfStack
1. Client-side validation
<script language="javascript">
img=new Image() ;
function Check_FileTypeAndFileSize()
{
// Get the upload path
var str=document.all.uploadFile.value;
// Verify that the upload path is not empty
if(str=="")
{
alert(" Please select the image to upload first! ");
return false;
}
// Verify that the uploaded file format is correct
var pos = str.lastIndexOf(".");
var lastname = str.substring(pos,str.length)
if (lastname.toLowerCase()!=".jpg" && lastname.toLowerCase()!=".gif")
{
alert(" The file type you uploaded is " lastname " , the picture must be JPG , GIF type ");
return false;
}
// Verify the uploaded file width/height ratio
if(img.width/img.height>1.6)
{
return confirm( The proportion of pictures you upload is greater than 8:5 Are you sure you want to upload? );
}
// Verify that the uploaded file is out of size
if(img.fileSize/1024>150)
{
alert(" The file size you uploaded is out of tolerance 150K Limit! ");
return false;
}
return true;
}
// Upload file box address change event, instant preview image
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">
<img id="imgPreview" src="images/pic1.gif" runat="server" />
</div>