Asp. Net upload image Verification Code

Source: Internet
Author: User

1. client-side Verification

  1. <Script language = "javascript">
  2. Img = new Image ();
  3. Function Check_FileTypeAndFileSize ()
  4. {
  5. // Obtain the upload path
  6. Var str = document. all. uploadFile. value;
  7. // Verify that the upload path is not empty
  8. If (str = "")
  9. {
  10. Alert ("select the image to upload first! ");
  11. Return false;
  12. }
  13. // Verify that the format of the uploaded file is correct
  14. Var pos = str. lastIndexOf (".");
  15. Var lastname = str. substring (pos, str. length)
  16. If (lastname. toLowerCase ()! = ". Jpg" & lastname. toLowerCase ()! = ". Gif ")
  17. {
  18. Alert ("the file type you uploaded is" lastname ", and the image must be JPG or GIF ");
  19. Return false;
  20. }
  21. // Verify the width and height of the uploaded file
  22. If (img. width/img. height & gt; 1.6)
  23. {
  24. Return confirm (the image you uploaded is larger than. Are you sure you want to upload it ?);
  25. }
  26. // Verify whether the size of the uploaded file has exceeded
  27. If (img. fileSize/1024> 150)
  28. {
  29. Alert ("the size of the file you uploaded exceeds the limit of KB! ");
  30. Return false;
  31. }
  32. Return true;
  33. }
  34. // Upload the file box address change event to preview the image instantly
  35. Function changephoto ()
  36. {
  37. Img. src = document. all. uploadFile. value;
  38. }
  39. // -->
  40. </Script>
  41. <Div>
  42. <Upload: InputFile ID = "uploadFile" name = "UploadFilePath" runat = "server" onchange = "changephoto ()"/>
  43. <Asp: Button ID = "btnUpload" runat = "server" Text = "Upload" OnClientClick = "return Check_FileTypeAndFileSize ()"
  44. OnClick = "btnUpload_Click"/>
  45. </Div>
  46. <Div class = "upload">
  47. </Div>

Related Article

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.