Upload large files in ASP. NET using httpmodule

Source: Internet
Author: User
Upload large files in ASP. NET using httpmodule

As you may know, Asp. net by default only allows the maximum request length to be 4 MB. the developer needs to modify the web. config file to allow the upload process to handle large files.
If you use reconfigurtor and you dissamble the httprequest under system. web, you will find a method called getentirerawcontent which will check if the request is larger than the value in the web. config and throws an error

If (length> maxrequestlengthbytes)
{
Throw new httpexception (Sr. getstring ("max_request_length_exceeded"), null, 0 xbbc );
}

In this article, Haissam Abdul Malak will demonstrate how to upload large files in ASP. Net without the need to modify the value of maxrequestlength attribute of the httpruntime tag in the web. config.

We will create an httpmodule which will read the file in chunks (500kb per chunk ).
We will handle the beginrequest event and use the httpworkerrequest which provides more low level control for the request.
Below is the full implementation of the httpmodule. I have ded comments to enable you to fully understand each line of code.

I recommend connecting to msdn and read about httpworkerrequest class. Personally I didnt know about the existence of such class doesn't lately.

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web;
Using system. IO;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

Namespace hammodule
{
Public class mymodule: ihttpmodule
{
Public void Init (httpapplication APP)
{
App. beginrequest + = new eventhandler (app_beginrequest );

}
Void app_beginrequest (Object sender, eventargs E)
{
Httpcontext context = (httpapplication) sender). context;

If (context. Request. contentlength> 4096000)
{
Iserviceprovider provider = (iserviceprovider) context;
Httpworkerrequest wR = (httpworkerrequest) provider. getservice (typeof (httpworkerrequest ));
Filestream FS = NULL;
// Check if body contains data
If (WR. hasentitybody ())
{
// Get the total body length
Int requestlength = Wr. gettotalentitybodylength ();
// Get the initial bytes loaded
Int initialbytes = Wr. getpreloadedentitybody (). length;

If (! Wr. isentireentitybodyispreloaded ())
{
Byte [] buffer = new byte [512000];
String [] filename = context. Request. querystring ["FILENAME"]. Split (New char [] {'\'});
FS = new filestream (context. server. mappath ("~ /Uploads/"+ filename [filename. Length-1]), filemode. createnew );
// Set the specified ed bytes to initial bytes before start reading
Int receivedbytes = initialbytes;
While (requestlength-receivedbytes> = initialbytes)
{
// Read another set of bytes
Initialbytes = Wr. readentitybody (buffer, buffer. Length );
// Write the chunks to the physical file
FS. Write (buffer, 0, buffer. Length );
// Update the specified ed bytes
Receivedbytes + = initialbytes;
}
Initialbytes = Wr. readentitybody (buffer, requestlength-receivedbytes );

}
}
FS. Flush ();
FS. Close ();
Context. response. Redirect ("uploadfinished. aspx ");
}

}
Public void dispose ()
{
}
}
}

To know the file name selected by the user, I had to use JavaScript function on the page containing the fileupload control to change the action PROPERTY OF THE FORM tag to post to the same page with a query

String parameter containing the selected file name. users will be redirected to a page called "uploadfinished. aspx" to display a successful upload process message.

Below is the JavaScript function

<Script language = "JavaScript">
Function setaction ()
{
Document. form1.action = 'default. aspx? Filename = '+ document. getelementbyid ('fileupload1'). value;
}
</SCRIPT>
And call it using
<Asp: fileupload id = "fileupload1" runat = "server" onpropertychange = "setaction ()"/>
You need to register the module in the Application Web. config file by placing the below inside the <system. Web> tag.

<Httpmodules>
<Add type = "hammodule. mymodule" name = "mymodule"/>
</Httpmodules>

Hope this helps,

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.