All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP脚本的8个技巧
C++封装与访问控制[二.二]
定义标题的最好方法_[Html教程]
Asp.net生成静态网页的实现代码_[Asp.Net教程]
深入理解C#,3.0的五项主要改进_.net资料_编程技术
ASP.NET技巧:將datagrid控件內容輸出到excel文件_[Asp.Net教程]
事半功倍之javascript,(1)_JavaScript技术_编程技术
从数据库导出数据到word、excel、.txt_[Asp.Net教程]
RSS制作(C#):使用XML,DOM_[Asp.Net教程]
visual c++中通用控件类成员
Reading number is top 10 pictures
China's zhejiang university to create the world's most light material
Sora aoi in China4
清醇靓丽的美眉
避免防盗门的猫眼变成钥匙眼
yy365网站上的美女3
30 beautiful school beauty4
何炅哥为中国人的平均工资鸣不平了
六种更聪明的工作方法
The girl of like self-time
Average female college students1
Download software ranking
Professional killers2 data package
linux高级编程
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
Unix video tutorial17
Boxer's Top ten classic battle3
Boxer vs Yellow3
功夫熊猫2(下集)
Sora aoi 120 minutes
Boxer's Top ten classic battle9
美女写真3
归海一刀 published in(发表于) 2014/1/30 0:59:30 Edit(编辑)
Asp.NET大文件上传组件开发总结(四)---封送数据给Asp.NET页面_[Asp.Net教程]

Asp.NET大文件上传组件开发总结(四)---封送数据给Asp.NET页面_[Asp.Net教程]

Asp.NET大文件上传组件开发总结(四)---封送数据给Asp.NET页面_[Asp.Net教程]

这个功能主要是为了不影响Asp.NET的处理模型,将请求中除上传的文件内容外的其它正常请求内容继续发送到Asp.NET页面处理。
我们通过分析HttpRequest对象的GetEntireRawContent()方法在读取数据,并且发现这个方法在读取数据时,是将数据赋值到了_rawContent属性,所以我们只要能把处理后的数据赋值给HttpRequest对象的_rawContent属性,即可封送数据给Asp.NET页面。同时,由于我们改写了请求内容,所以HttpRequest的_contentLength也应当改写。


由于这些属性方式是私有的,我们不能直接访问,所以我们必须采用反射的方法给属性赋值。代码如下:



private void InjectTextParts(HttpRequest request, byte[] textParts)
{
BindingFlags flags1 = BindingFlags.NonPublic | BindingFlags.Instance;
Type type1 = request.GetType();
FieldInfo info1 = type1.GetField("_rawContent", flags1);
FieldInfo info2 = type1.GetField("_contentLength", flags1);

if ((info1 != null) && (info2 != null))
{
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();


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);
}
}



在这里,在将代码用到NET2.0时,遇到了问题。因为在NET1.1时,HttpRequest的_rawContent属性是一个byte[]类型,但到了NET2.0,这个属性变成了HttpRawUploadedContent类型的对象,出现了赋值失败。通过查看HttpRawUploadedContent反射代码,发现原来这个类是为了将过大的请求内容写到磁盘文件中。没办法,我只有创建这个类的实例后再赋值给HttpRequest的_rawContent属性。这下能上传文件了,可是,Asp.NET页面的控件值全部丢失了,数据没有有效的封送到页面。嘿嘿,这里正好发挥我刚学会的一项技术---调试框架源码。于是从GetEntireRawContent()方法一路跟踪下去,发现是由于HttpRawUploadedContent对象的_length属性为零,导致HttpRequest对象认为没有有效数据而没有分析数据。导致通过HttpRequest的Params属性和Forms数据不能访问到请求内容。给这两个数据赋值后,哈!哈!一切顺利。上传过程全部结束!
由于NET2.0代码只是初步完成,等我在作进一步测试后,将在写完这部分总结后,提供给大家下载。







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.