HTML input (File) allows you to upload images to determine the image size and type.

Source: Internet
Author: User

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 );
}
}

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.