Upload Large Files in ASP.NET Using HttpModule
2010-02-22 14:31
615 查看
AS you may know, ASP.NET by default only allows the maximum request length to be 4MB. The developer needs to modify the web.config file to allow the upload process to handle large files.
If you use Reflactor 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, 0xbbc);
}
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 included 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 except lately.
代码
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,
但是这种方法不能在IE下使用,FF下正常。
在IE下 wr.GetPreloadedEntityBody() 为 null
原文地址:http://dotnetslackers.com/Community/blogs/haissam/archive/2008/09/12/upload-large-files-in-asp-net-using-httpmodule.aspx
If you use Reflactor 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, 0xbbc);
}
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 included 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 except 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 received 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 received 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,
但是这种方法不能在IE下使用,FF下正常。
在IE下 wr.GetPreloadedEntityBody() 为 null
原文地址:http://dotnetslackers.com/Community/blogs/haissam/archive/2008/09/12/upload-large-files-in-asp-net-using-httpmodule.aspx
相关文章推荐
- Upload Large Files in ASP.NET Using HttpModule
- http://venkatbaggu.com/file-upload-in-asp-net-mvc-using-dropzone-js-and-html5/
- File Upload using jQuery AJAX in ASP.NET Web API or Http handler (AJAX上传文件通过Web API或 http handler)
- Upload Multiple Files in ASP.NET using jQuery
- System.TypeLoadException for System.ServiceModel.Activation.HttpModule in ASP.NET 4
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)
- Part 17 Consuming ASP NET Web Service in AngularJS using $http
- asp.net upload large files with swfupload 2.2
- using swfUpload in asp.net mvc
- 【转】In ASP.NET using jQuery Uploadify upload attachment
- Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control
- Multiple Files Upload in ASP.NET and ASP.NET with jQuery
- Upload Files Using ASP.NET Impersonation and UNC Share
- asp.net 大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)
- 【转载】在ASP.NET 2.0 中如何使用多个Sitemap文件【Using Multiple Sitemap Files in ASP.NET 2.0--英文】
- Upload Large File In asp.net or website in Windows Azure .
- Storing Binary Files Directly in the Database Using ASP.NET 2.0
- Preview Word files (docx) in HTML using ASP.NET, OpenXML and LINQ to XML