我正在使用AsyncFileUpload控件上传图片。 它从localhost可以正常工作,但是当我将它上传到服务器上时,我收到以下错误。
我甚至无法理解这个错误的原因。我会感激任何答案。
这是我正在使用的方法
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
string email = WriteYourEmailTXT.Text;
var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToString("ddMMyyyy");
string parentFolder = Server.MapPath("uploads");
string childFolder = parentFolder + "\\" + email + "\\";
if (!Directory.Exists(childFolder))
Directory.CreateDirectory(childFolder);
string counter = getCount();
if (AsyncFileUpload1.HasFile) {
string ext = getExtension(AsyncFileUpload1.FileName);
string finalName = dt + "_" + counter + ext;
string finalPath = childFolder + finalName;
AsyncFileUpload1.SaveAs(finalPath);
SetCount();
SetFileName(finalName);
}
}
protected void SetCount()
{
HttpCookie aCookie = Request.Cookies["CountPhoto"];
int cookieNum = 0;
if (aCookie != null)
if (aCookie.Value != "")
cookieNum = int.Parse(aCookie.Value.ToString());
string newvalue = (cookieNum + 1).ToString();
Response.Cookies["CountPhoto"].Value = newvalue;
}
protected void SetFileName(string fileName)
{
HttpCookie aCookie = Request.Cookies["FileName"];
var filename = "";
if (aCookie != null)
if (aCookie.Value != "") {
filename = aCookie.Value;
}
string newFileName = filename + "," + fileName;
Response.Cookies["FileName"].Value = newFileName;
}
错误
无法序列化会话状态。 在'StateServer'和'SQLServer'模式下, ASP.NET将序列化会话 状态对象,因此 不可序列化的对象或 MarshalByRef对象不是 允许的。同样的限制 适用于类似的序列化 由自定义会话状态存储完成 在“自定义”模式下。
堆栈跟踪: [SerializationException:Type 'System.Web.HttpPostedFile'中 Assembly'System.Web,Version = 2.0.0.0, 文化=中性, PublicKeyToken = b03f5f7f11d50a3a'是 未标记为可序列化。]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType 型号)+7733643
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(类型 type,StreamingContext context)+258
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +111 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext context,SerObjectInfoInit serObjectInfoInit,IFormatterConverter 转换器,ObjectWriter objectWriter) +161 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj,ISurrogateSelector surrogateSelector,StreamingContext context,SerObjectInfoInit serObjectInfoInit,IFormatterConverter 转换器,ObjectWriter objectWriter) +51 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph,Header [] inHeaders, __BinaryWriter serWriter,Boolean fCheck)+410
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流 serializationStream,对象图, Header [] headers,Boolean fCheck)+134 System.Web.Util.AltSerialization.WriteValueToStream(对象 value,BinaryWriter writer)+1577[HttpException(0x80004005):无法执行 序列化会话状态。在 'StateServer'和'SQLServer'模式, ASP.NET将序列化会话 状态对象,因此 不可序列化的对象或 MarshalByRef对象不是 允许的。同样的限制 适用于类似的序列化 由自定义会话状态存储完成 在“自定义”模式下。]
System.Web.Util.AltSerialization.WriteValueToStream(对象 值,BinaryWriter作者)+1662
System.Web.SessionState.SessionStateItemCollection.WriteValueToStreamWithAssert(对象 值,BinaryWriter作者)+34
System.Web.SessionState.SessionStateItemCollection.Serialize(的BinaryWriter 作家)+606
System.Web.SessionState.SessionStateUtility.Serialize(SessionStateStoreData item,Stream stream)+239
System.Web.SessionState.SessionStateUtility.SerializeStoreData(SessionStateStoreData item,Int32 initialStreamSize,Byte []& buf,Int32&长度)+72
System.Web.SessionState.OutOfProcSessionStateStore.SetAndReleaseItemExclusive(HttpContext的 context,String id, SessionStateStoreData项,Object lockId,Boolean newItem)+87
System.Web.SessionState.SessionStateModule.OnReleaseState(对象 source,EventArgs eventArgs)+560
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步,布尔& completedSynchronously) 75
答案 0 :(得分:1)
问题在于本地计算机与Web服务器之间的IIS配置差异。
您的服务器似乎设置为使用StateServer(另一台PC处理内存中的大量用户数据)或使用SQL Server(您的数据库将用户对象存储在特殊表中)来处理会话。为了将数据传输到State Server或数据库服务器,IIS将序列化您的会话信息以将其传输到其他服务器。基本上,这会获取给定对象的所有细节并将它们转换为XML,以便可以根据需要将对象“保存”并“加载”到内存中。
这种情况下的具体问题是'System.Web.HttpPostedFile'类不支持序列化。通常这个问题出现在由我们自己的开发人员编写的类中,我们可以只向类中添加一个属性,但是在这里看来问题存在于.NET框架提供的类中。我担心你需要找到解决问题的方法,比如写自己的课或不使用这个。