Asp.net Image Upload and display

Source: Internet
Author: User

 

Code

Protected void button#click (object sender, EventArgs e)
{
String fileName = this. File1.PostedFile. FileName; // get the client file name

String type = fileName. Substring (fileName. LastIndexOf (".") + 1). ToLower (); // get the file suffix

If (type = "jpg" | type = "gif" | type = "jpeg") // determine the file format
{
String savePath = Server. MapPath ("~ /Upload/"); // specifies the storage path of the uploaded file on the server.

If (! Directory. Exists (savePath) // check whether the physical path Exists on the server.
{
Directory. CreateDirectory (savePath); // create a Directory if it does not exist
}

This. File1.PostedFile. SaveAs (savePath + "\" + fileName); // upload

This. Image1.ImageUrl = "Image/" + filename; // The Image is displayed after the upload is successful.

}
Else
{
Response. Write ("<Script language = 'javascript '> alert ('format error') </Script> ");
}


}

 

 

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.