您的位置:首页 > 编程语言 > ASP

Asp.Net 上传大文件专题(3)--从请求流中获取数据并保存为文件[下]

2008-08-03 21:39 746 查看
接着上一篇

3.4 读取剩余的请求

前面我们已经提到过ReadEntityBody (Byte[] buffer, Int32 size)方法,该方法可以用来读取客户端的请求数据。我们想要读取剩余部分的请求数据,就是要使用这个方法来从异名管道中循环取出请求。 [buffer:将数据读入的字节数组;size:最多读取的字节数;如果所被读取的剩余请求字节长度小于size,那么该方法会将多余的size大小的字节数组用0填充,这样会损失不必要的性能,因此我们在使用该方法前最好先判断下剩余的HTTP请求大小与size的关系。据其他前辈们测试该方法大多数读取的数据长度都在8192左右,所以size不必定的很大。]

while (iLeave > iReadStepSize && request.IsClientConnected())

if (iLeave > 0 && request.IsClientConnected())

private byte[] GetMultipartBoundary()

private void WriteHttpRequestWithoutFileData(byte[] bWriteByte,int iStartIndex, int iLength,ref byte[] bHttpRequestByte)

private byte[] UnionByte(byte[] bnewbyte, byte[] boldbyte)

private void InjectTextParts(HttpRequest request, byte[] textParts)

{

{

Assembly web = Assembly.GetAssembly(typeof(HttpRequest));

Type hraw = web.GetType("System.Web.HttpRawUploadedContent");

object[] argList = new object[2];

argList[0] = textParts.Length + 1024;

argList[1] = textParts.Length;

CultureInfo currCulture = CultureInfo.CurrentCulture;

object httpRawUploadedContent = Activator.CreateInstance(hraw,

BindingFlags.NonPublic | BindingFlags.Instance,

null,

argList,

currCulture,

null);

Type contentType = httpRawUploadedContent.GetType();

contentType.GetField("_completed", flags1).SetValue(httpRawUploadedContent, true);//不加上这句就会有问题

FieldInfo dataField = contentType.GetField("_data", flags1);

dataField.SetValue(httpRawUploadedContent, textParts);

FieldInfo lengthField = contentType.GetField("_length", flags1);

lengthField.SetValue(httpRawUploadedContent, textParts.Length);

FieldInfo fileThresholdField = contentType.GetField("_fileThreshold", flags1);

fileThresholdField.SetValue(httpRawUploadedContent, textParts.Length + 1024);

info1.SetValue(request, httpRawUploadedContent);

info2.SetValue(request, textParts.Length);

}

}

4 在aspx页面中加入一个<input type="file" id="FileUpload1" name="file" />,并修改Form的enctype = " multipart/form-data " , 测试一下吧

好了,到这差不多就基本完成了,但是这样只是实现了文件的上传,并没有发挥HTTP模块真正的魅力。接下来一篇将向大家介绍如何实现进度条的显示。

下一篇
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐