ASP. NET solution for uploading large files

  • 2021-07-01 07:09:01
  • OfStack

First, we need to download this component named RanUpLoad.

After the download is complete, the two dll files are added to the reference center of the project, and the xml file is copied under the bin folder in the project, that is, the last three files exist in the bin folder.

Next, upload the control with the FileUpload control that comes with ASP. NET. What needs to be added is to add a label next to the FileUpload control:


<radU:RadProgressManager ID="Radprogressmanager1" Width="100%" runat="server" />
<radU:RadProgressArea ID="progressArea1" Width="100%" runat="server">
</radU:RadProgressArea>

And add the following code at the beginning of the aspx file:


<%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" Assembly="Telerik.QuickStart" %>
<%@ Register TagPrefix="radU" Namespace="Telerik.WebControls" Assembly="RadUpload.Net2" %>

Of course, the configuration file's < system.web > You can't forget the following statements in the tag:


<httpRuntime executionTimeout="3600" maxRequestLength="2097151" ></httpRuntime>
<httpModules>
    <add name="RadUploadModule" type="Telerik.WebControls.RadUploadHttpModule, RadUpload.Net2"/>
</httpModules>
<httpHandlers>
    <add verb="*" path="Telerik.RadUploadProgressHandler.aspx" type="Telerik.WebControls.RadUploadProgressHandler, RadUpload.Net2"></add>
</httpHandlers>

Now that the external outline has been laid out, the next step is the operation required by the server after clicking Upload:

Of course, before doing this, let's start with the Telerik. WebControls namespace under using 1.


//  Check files 
if (RadUploadContext.Current == null) { return; }
if (RadUploadContext.Current.UploadedFiles.Count <= 0) 
{
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert(' Please select Upload File   ! ')</script>"); 
        return;
}
if (RadUploadContext.Current.UploadedFiles[0].ContentLength >= 2147483647)
{
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "MsgBox", "<script>alert(' Uploaded files must not exceed  2GB  ! ')</script>");
        return;
}
UploadedFile file = RadUploadContext.Current.UploadedFiles[0];
string fileName = Path.GetFileName(file.FileName);
string virtualPath = System.IO.Path.Combine("~/save", fileName);
string savePath = this.MapPath(virtualPath);
file.SaveAs(savePath, true);   

At this point, the file upload processing work has been completed, the above cs code is my own 1 operation processing, you can modify it according to your own situation, for example, you can also place multiple FileUpload controls,

Uploading of multiple files is handled in the manner of foreach (UploadedFile file in RadUploadContext. Current. UploadedFiles) {...}.

I hope this article can help friends who have a headache in uploading large files to easily deal with uploading problems.


Related articles: