The file upload control is: FileUpload.
Asp. Net has a limit on the size of the uploaded files. By default, users can only upload 4 MB files, which may cause inconvenience to users. Therefore, if you want to upload a 40 MB file. Only configuration files can be modified
The key code is as follows:
Copy codeThe Code is as follows:
Protected void btnSend_Click (object sender, EventArgs e)
{
Try
{
// Idea of uploading files:
// Get the name of the uploaded file, which is a full path address
String upFileName = fulFileName. FileName;
// Obtain the extension of the uploaded file
String lastName = upFileName. Substring (upFileName. LastIndexOf ("."));
// Get the new file name
String newFileName = txtFileName. Text + lastName;
// Set the path of the file to be saved
String FilePath = Server. MapPath ("./") + "File" + "//" + newFileName;
// Save the file to the specified file path
FulFileName. PostedFile. SaveAs (FilePath );
LblResult. Text = "uploaded successfully ";
}
Catch (Exception ex)
{
Response. Write (ex. Message. ToString ());
LblResult. Text = "Upload Failed ";
}
}
Key train of thought: 1. Get the original name of the uploaded file first, 2. Get the extension of the uploaded file to form a new name. 3. Set the path to be saved to use: Server. mapPath (". /") +" File "+ newFileName. /indicates the virtual path on the current page. File indicates that the File folder on the current page must be created first. 4. Save the File to the specified File path. Use the SaveAs () method of the PostFile of the FileUpload Control
To upload large files, you must modify the Web. config file.
<System. web>
<HttpRuntime maxRequestLength = "40960" executionTimeOut = "6000"/>
</System. web>
Key setting: maxRequestLength. This attribute indicates the maximum number of bytes uploaded.