Simple File Upload implementation for Asp. Net File Upload

Source: Internet
Author: User

FileUpload Control

Attribute:
FileName: Get the uploaded file name

HasFile: Specifies whether to select (exist) the file to be uploaded.

ContentLength: obtains the size of the climbing object, in bytes)

Method:
Server. MapPath (): gets the physical path on the Server.
SaveAs (): Save the file to the specified folder.

Note: by default, the size of the uploaded file is limited to 4 MB. You can modify the default setting through web. config. comments (which is a global configuration ).
You can also modify the web. config file to change the application upload restrictions.

Protected void button#click (object sender, EventArgs e)
{
If (FileUpload1.HasFile)
{
If (FileUpload1.PostedFile. ContentLength <10485760) // checks whether the file is smaller than 10 Mb.
{
Try
{
// Upload the file and specify the path of the upload directory
FileUpload1.PostedFile. SaveAs (Server. MapPath ("~ /Images/") + FileUpload1.FileName );
/* Note-> Why not: FileUpLoad1.PostedFile. FileName
* But: FileUpLoad1.FileName?
* The former is the name that obtains the complete limitation of the client (full path of the client ).
* The latter FileUpLoad1.FileName only obtains the file name.
*/
// Of course, the upload statement can also be written like this (seemingly nonsense ):
// FileUpLoad1.SaveAs (@ "D: \" + FileUpLoad1.FileName );
LblMessage. Text = "Upload successful! ";
}
Catch (Exception ex)
{
LblMessage. Text = "An exception occurred and cannot be uploaded! ";
// LblMessage. Text + = ex. Message;
}
}
Else
{
LblMessage. Text = "the size of the uploaded file cannot exceed 10 MB! ";
}
}
Else
{
LblMessage. Text = "file not selected! ";
}
}

Related Article

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.