Asp.net is suitable for beginners to upload files.

Source: Internet
Author: User

Asp tutorial. net is suitable for beginners to upload files

Today, I made a. aspx page for uploading images from a browser. Of course, after modification, I can also upload files in other formats. The following code is attached:

-- Put a file upload control and a button on the page, and click the button to trigger the content.

// Obtain the file path
String filePath = fileBookPhoto. PostedFile. FileName;
If (string. IsNullOrEmpty (filePath ))
{
Page. ClientScript. RegisterStartupScript (GetType (), "", "alert ('select an image! ') ", True );
Return;
}
// Restrict the format of the uploaded file

// Create a file object based on the complete path of the client file
FileInfo file = new FileInfo (filePath );
// Get the file suffix
String fileType = file. Extension;
// Response. Write ("file type:" + fileType );
If (fileType. equals (". jpeg ") | fileType. equals (". jpg ") | fileType. equals (". gif ") | fileType. equals (". bmp ") | fileType. equals (". png "))
{
// Save the file to the specified directory
// Get the file Name (file. Name)
FileBookPhoto. SaveAs (Server. MapPath ("~ /Image/"+ file. Name ));
// Display the image
// This. imgBookPhoto. ImageUrl = "~ /Image/"+ file. Name;
Book. Photo = file. Name;
}
Else
{
Page. ClientScript. RegisterStartupScript (GetType (), "", "alert ('the image format is incorrect! ') ", True );
Return;
}

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.