public void nextPageButton_Click(object sender, EventArgs e)
2 {
3 int fileLength;
4 Stream objStream;
5
6 if (checkBox1.Checked)
7 {
8 if (uplFile.HasFile)
9 {
10 fileLength = uplFile.PostedFile.ContentLength;
11 objStream = uplFile.PostedFile.InputStream;
12 Session["FILE_docs"] = objStream;
13 }
14 else
15 {
16 Session["FILE_docs"] = null;
17 }
18 }
19 }
出现以下错误:session不支持非序列化对象!
如何更改,我也知道用wizard在同一页面处理比较好些,最后点按钮提交,不过想搞清楚序列化存储问题,所以故意写在好几个页面内,中间用session存储,经过n个页面,最后点击按钮上传至服务器。Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
2 {
3 int fileLength;
4 Stream objStream;
5
6 if (checkBox1.Checked)
7 {
8 if (uplFile.HasFile)
9 {
10 fileLength = uplFile.PostedFile.ContentLength;
11 objStream = uplFile.PostedFile.InputStream;
12 Session["FILE_docs"] = objStream;
13 }
14 else
15 {
16 Session["FILE_docs"] = null;
17 }
18 }
19 }
出现以下错误:session不支持非序列化对象!
如何更改,我也知道用wizard在同一页面处理比较好些,最后点按钮提交,不过想搞清楚序列化存储问题,所以故意写在好几个页面内,中间用session存储,经过n个页面,最后点击按钮上传至服务器。Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
http://blog.csdn.net/HeddaZ/archive/2008/04/23/2317343.aspx
有个heddawindow.dll 用reflector打开,里面有个把流转换成字符串的方法
有点扯远了, 我目前贴不上来,我也找不到备份了,呵呵,不过,这个方法很简单的自己写也快
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Session["FILE_docs"]); }
public void nextPageButton_Click(object sender, EventArgs e)
{
int fileLength;
System .IO . Stream objStream;
if (uplFile.HasFile)
{
fileLength = uplFile.PostedFile.ContentLength;
objStream = uplFile.PostedFile.InputStream;
Session["FILE_docs"] = objStream;
Response.Write("Yes");
}
else
{
Session["FILE_docs"] = null;
Response.Write("No");
}
}经测试,单个页面,以上代码没有问题
是不是将objStream 转成文件的时候出了问题
这样就可以
序列化看似挺神秘,其实真正用起来就很简单了(只限于普通应用,不重写如何序列化和反序列化)。你写的测试例子,可以按1,7楼兄弟意见,用流把上传文件写道字节数组就可以存到session里了。但个人认为用这个例子测试序列化有点复杂,而且也不明显,我举了个例子,供大家参考,如果不好,大家可别轰.... :)
所有.net默认的基本类型都是可序列化和反序列化的,所以可以放到session里。所以你可以定义一个A类,有a,b,c三个属性,分别为int,string,bool类型,然后你new 一个A类实例objA,先把objA放到Session里运行,结果发现错误,A类不可序列化,因为A是用户自定义类型,.net不知道如果序列化A类,所以报错,如果要将A的实例放到Session里,最简单的方法是把A类标记为可序列化的,在声明A类的上面加上[Serializable]就可,这样objA就可以放到Session里了,如果你又声明一个D类,在A类里加个d属性,类型是D类型,那要把D类也标记为可序列化,那样objA才能放到Session里,依此类推,如果还有别的欠套,那需要标记完其他类,objA才能放到Session里,即是可序列化的类。
其实我最多就用到这层次,没有写过实现ISerializable接口,自定义序列化和反序列化。不知道看明白否,仅供参考:)
BinaryReader br = new BinaryReader(objStream);
Session["FILE_docs"] = br.ReadBytes(1024*1024*10); // byte[]