Front-end code:
<Form ID = "form1" runat = "server">
<Br/>
<Input id = "fileupload" type = "file" runat = "server"/> size: 130*140 <br/>
<Asp: button id = "button1" runat = "server" text = "Upload" onclick = "button#click1"
Style = "height: 26px"/>
</Form>
Background code:
Protected void button1_click1 (Object sender, eventargs E)
{
// Obtain the full path name of the file on the client computer (for example, "d:/myfiles/lo.txt ")
String fullfilename = This. fileupload. postedfile. filename;
// Obtain the specific file name (for example, "lo.txt ")
String filename = fullfilename. substring (fullfilename. lastindexof (@ "/") + 1 );
// Obtain the file extension (for example, "TXT ")
String typename = (fullfilename. substring (fullfilename. lastindexof (".") + 1). tolower ();
// Determine whether the format of the uploaded file is image format
// Upload If yes
If (typename = "jpg" | typename = "BMP" | typename = "GIF" | typename = "Jpeg ")
{
If (validatefiledimensions ())
{
This. fileupload. postedfile. saveas (server. mappath ("~ /Productimages/") + filename); // + @"/"
Response. Write ("uploaded successfully ");
}
Else
{
Response. Write ("the image size does not meet the requirements ");
}
}
Else
{
Response. Write ("<SCRIPT syntax Ge = 'javascript '> alert ('your image format is incorrect! '); </SCRIPT> ");
}
}
// Determine the image size
Public bool validatefiledimensions ()
{
Using (system. Drawing. Image myimage = system. Drawing. image. fromstream (fileupload. postedfile. inputstream ))
{
Return (myimage. Height = height & myimage. width = width );
}
}