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代码只是初步完成,等我在作进一步测试后,将在写完这部分总结后,提供给大家下载。