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! ";
}
}