Tutorial ASP. NET uploads images, server verification, width, height, file type, size, file size, Image Upload, and image format check.
First, add
// <INPUT style = "WIDTH: 272px; HEIGHT: 22px "type =" file "size =" 26 "name =" picfile "id =" picfile "runat =" server ">
The following definitions are made in the Code:
// Protected System. Web. UI. HtmlControls. HtmlInputFile picfile;
// Check and upload
Private bool CheckPic ()
{
Bool isSafe = false;
HttpPostedFile pic = picfile. PostedFile;
// Check the extension
String picext = System. IO. Path. GetExtension (picfile. PostedFile. FileName). ToLower ();
If (picext = ". jpg" | picext = ". gif" | picext = "bmp" | picext = "png ")
{
IsSafe = true;
}
Else
{
IsSafe = false;
Kit. Alert (this. Page, "the image format is incorrect. Please convert it to the common image format jpg, gif, bmp, png ");
Return isSafe;
}
// Check the image size
If (pic. ContentLength> PicMaxLength ())
{
IsSafe = false;
Kit. Alert (this. Page, "exceeds the image size limit ");
Return isSafe;
}
// Image size check
System. IO. Stream picstream = pic. InputStream;
System. Drawing. Image img = System. Drawing. Image. FromStream (picstream );
If (img. Width> 0 & img. Height> 0)
{
IsSafe = true;
// Step 3: Verify the ID card image and upload it
SaveIdcard (txtidcard. Text. Trim ());
Picstream. Close ();
Picstream. Flush ();
}
Else
{
IsSafe = false;
Kit. Alert (this. Page, "invalid image file ");
// Picstream. Close ();
// Picstream. Flush ();
Return isSafe;
}
Return isSafe;
}