如何上传大文件,找了很多资料,看不懂某些代码,一个人开发的小项目,很急!谢谢!

patrickjiang patrickjiang 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
工作上需要实现一个大文件上传功能,我找了好多资料,采用了以下方法,但我不太懂,所以求助各位兄弟,谢谢了!!!

//ASPX页面中的文件上传FileUpload、提交按钮Button、结果Label
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" /><br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><br /><br />
<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
</div>
</form>



//ASPX页面的C#代码
protected void Button1_Click(object sender, EventArgs e)
{
string strDesPath = "D:\\";
string strFileName = FileUpload1.PostedFile.FileName;
strFileName = strDesPath + strFileName;

FileUpload1.PostedFile.SaveAs(strFileName);
Label1.Text = "文件保存到了:" + strFileName;
}


以上是页面文件,以下是一个HttpUploadModule类,实现了IHttpModule接口,另外还有一个web.config文件代码:

public class HttpUploadModule : IHttpModule
{
public HttpUploadModule()
{
}

public void Init(HttpApplication application)
{
//订阅事件
application.BeginRequest += new EventHandler(this.Application_BeginRequest);
}

public void Dispose()
{
}

private void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
HttpWorkerRequest request = GetWorkerRequest(app.Context);
Encoding encoding = app.Context.Request.ContentEncoding;

int bytesRead = 0; // 已读数据大小
int read; // 当前读取的块的大小
int count = 8192; // 分块大小
byte[] buffer; // 保存所有上传的数据

if (request != null)
{
// 返回 HTTP 请求正文已被读取的部分。
byte[] tempBuff = request.GetPreloadedEntityBody(); //要上传的文件

// 如果是附件上传
if (tempBuff != null && IsUploadRequest(app.Request)) //判断是不是附件上传
{
// 获取上传大小
//
long length = long.Parse(request.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength));

buffer = new byte[length];
count = tempBuff.Length; // 分块大小

// 将已上传数据复制过去
//
Buffer.BlockCopy(tempBuff, //源数据
0, //从0开始读
buffer, //目标容器
bytesRead, //指定存储的开始位置
count); //要复制的字节数。


// 开始记录已上传大小
bytesRead = tempBuff.Length;

// 循环分块读取,直到所有数据读取结束
while (request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded() && bytesRead < length)
{
// 如果最后一块大小小于分块大小,则重新分块
if (bytesRead + count > length)
{
count = (int)(length - bytesRead);
tempBuff = new byte[count];
}

// 分块读取
read = request.ReadEntityBody(tempBuff, count);

// 复制已读数据块
Buffer.BlockCopy(tempBuff, 0, buffer, bytesRead, read);

// 记录已上传大小
bytesRead += read;

}
if (request.IsClientConnected() && !request.IsEntireEntityBodyIsPreloaded())
{
// 传入已上传完的数据
InjectTextParts(request, buffer);
}
}
}
}


HttpWorkerRequest GetWorkerRequest(HttpContext context)
{

IServiceProvider provider = (IServiceProvider)HttpContext.Current;
return (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
}

/// <summary>
/// 传入已上传完的数据
/// </summary>
/// <param name="request"></param>
/// <param name="textParts"></param>
void InjectTextParts(HttpWorkerRequest request, byte[] textParts)
{
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;

Type type = request.GetType();

while ((type != null) && (type.FullName != "System.Web.Hosting.ISAPIWorkerRequest"))
{
type = type.BaseType;
}

if (type != null)
{
type.GetField("_contentAvailLength", bindingFlags).SetValue(request, textParts.Length);
type.GetField("_contentTotalLength", bindingFlags).SetValue(request, textParts.Length);
type.GetField("_preloadedContent", bindingFlags).SetValue(request, textParts);
type.GetField("_preloadedContentRead", bindingFlags).SetValue(request, true);
}
}

private static bool StringStartsWithAnotherIgnoreCase(string s1, string s2)
{
return (string.Compare(s1, 0, s2, 0, s2.Length, true, CultureInfo.InvariantCulture) == 0);
}

/// <summary>
/// 是否为附件上传
/// 判断的根据是ContentType中有无multipart/form-data
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
bool IsUploadRequest(HttpRequest request)
{
return StringStartsWithAnotherIgnoreCase(request.ContentType, "multipart/form-data");
}
}



<configuration>
<connectionStrings>
<add name="TrainingExamConnectionString" connectionString="Data Source=JIANGPENG\SQLEXPRESS;Initial Catalog=TrainingExam;User ID=sa;Password=2112706"
providerName="System.Data.SqlClient" />
<add name="TrainingExamConnectionString2" connectionString="Data Source=KYB-PC\SQLEXPRESS;Initial Catalog=TrainingExam;User ID=sa;Password=2112706"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.webServer>
<modules>
<add name="HttpUploadModule" type="k2hTEWebApp.HttpUploadModule, k2hTEWebApp"></add>
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
</configuration>


...全文

相关推荐

java web 大文件上传源代码,找了好久改了下,这是最完整的

java web 大文件上传源代码,找了好久,分享给大家,加入了缺少的jar包,这是目前为止找到的最为完整的项目了,所有的java类,jar包,数据表脚本全部都齐全了。

activexme

java实现大文件上传并有进度条及其代码解析

java实现大文件上传并有进度条及其代码解析

mine_song

java实现大文件上传分片上传断点续传.zip

java实现的大文件上传,支持断点续传 支持分片分片上传 采用springboot框架是实现

slinvoyage

使用HttpWebRequest实现大文件上传

通常在WinForm程序中都是采用WebClient方式实现文件上传功能,本身这个方式没有问题,但是当需要上传大文件比如说(300+M)的时候,那么WebClient将会报内存不足异常(Out of Memory Exceptions),究其原因是因为WebClient方式是一次性将整个文件全部读取到本地内存中,然后再以数据流形式发送至服务器。本文将讲述如何采用HttpWebRequest方式每次读取固定大小数据片段(如4KB)发送至服务器,为大文件上传提供解决方案,本文还将详细讲述如何将“文件上传”功能做为用户自定义控件,实现模块重用。

飞天心宏

大文件上传支持断点续传springboot版

大文件上传的一个小例子,springboot框架集成mybatis,数据库是mysql。采用分片上传,理论支持无限大小,实际测试过1.5G左右大小的文件。

一切都只是开始
评论 15
收藏 已收藏
分享
返回顶部
patrickjiang
.NET社区 62,275 成员 668,968 内容