ASP.NET's solution to the problem of uploading large files

  • 2020-05-27 04:44:24
  • OfStack

The upload file control is: FileUpload

Asp.Net has a limit on the size of the files you can upload. By default, users can only upload files of 4MB size, which is inconvenient for users. So if you want to upload a 40MB file. Only configuration files can be modified

The key code is as follows


 protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            // Ideas for uploading files: 
            // Gets the name of the uploaded file, which is 1 Full path address 
            string upFileName = fulFileName.FileName;
            // Gets the extension for the uploaded file 
            string lastName = upFileName.Substring(upFileName.LastIndexOf("."));
            // Get the new file name 
            string newFileName = txtFileName.Text + lastName;
            // Set the path to the file you want to save to 
            string FilePath =Server.MapPath("./")+"File"+"//"+newFileName;
            // Saves 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 ideas for uploading files: 1. Get the original name of the uploaded file first; 2. Get the extension of the uploaded file to form a new name. Server.MapPath ("./ ")+ "File" + newFileName. /) represents the virtual path under the current page, and File means the File folder in the current page must be created first. SaveAs () method of PostFile using the FileUpload control

To upload large files, you need to modify the Web.config file.

< system.web >

< httpRuntime maxRequestLength = "40960"/executionTimeOut = "6000" >

< /system.web >

The key is set to maxRequestLength, which represents the maximum bytes uploaded


Related articles: