在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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 (length > maxRequestLengthBytes)
We will create an HttpModule which will read the file in chunks (500KB per chunk). 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"> <httpModules> Hope this helps, |
请发表评论