有人可以解释为什么无法从 PageMethod 触发下载吗?
我有以下被称为客户端的 PageMethod。据我所知它应该完全有效,但什么也没发生。我能够从另一个 SO 成员那里引出我需要使用 ASHX 文件而不是这个实现,但那是另一天的事了......现在,我希望更好地了解幕后发生的事情,使得下载以这种方式文件不是一个选择吗?谢谢。
[WebMethod]
public static bool Export()
{
bool successful = false;
try
{
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=Dashboard.xml");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = true;
SerializableDictionary<string, string> dataToSave = new SerializableDictionary<string, string>();
foreach( var state in StateManager.StateDictionary)
{
var sessionItem = SessionRepository.Instance.GetSession(state.Value);
if (Equals(sessionItem, null)) continue;
using( MemoryStream memoryStream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(sessionItem.GetType());
serializer.Serialize(memoryStream, sessionItem);
dataToSave.Add(state.Value, Convert.ToBase64String(memoryStream.ToArray()));
}
}
XmlSerializer xmlSerializer = new XmlSerializer(dataToSave.GetType());
using( MemoryStream memoryStream = new MemoryStream())
{
xmlSerializer.Serialize(memoryStream, dataToSave);
HttpContext.Current.Response.AppendHeader("Content-Length", memoryStream.Length.ToString());
memoryStream.Position = 0;
using( StreamReader streamReader = new StreamReader(memoryStream))
{
HttpContext.Current.Response.Write(streamReader.ReadToEnd());
}
}
HttpContext.Current.Response.Flush();
successful = true;
}
catch (Exception exception)
{
_logger.ErrorFormat("Unable to serialize session. Reason: {0}", exception.Message);
}
return successful;
}
I have the following PageMethod which is being called client-side. As far as I know it should work completely, but instead nothing happens. I was able to elicit out of another S.O. member that I need to use an ASHX file instead of this implementation, but that's for another day... for now, I was hoping to better understand whats going on behind the scenes that makes downloading a file in this way not an option? Thanks.
[WebMethod]
public static bool Export()
{
bool successful = false;
try
{
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment; filename=Dashboard.xml");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = true;
SerializableDictionary<string, string> dataToSave = new SerializableDictionary<string, string>();
foreach( var state in StateManager.StateDictionary)
{
var sessionItem = SessionRepository.Instance.GetSession(state.Value);
if (Equals(sessionItem, null)) continue;
using( MemoryStream memoryStream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(sessionItem.GetType());
serializer.Serialize(memoryStream, sessionItem);
dataToSave.Add(state.Value, Convert.ToBase64String(memoryStream.ToArray()));
}
}
XmlSerializer xmlSerializer = new XmlSerializer(dataToSave.GetType());
using( MemoryStream memoryStream = new MemoryStream())
{
xmlSerializer.Serialize(memoryStream, dataToSave);
HttpContext.Current.Response.AppendHeader("Content-Length", memoryStream.Length.ToString());
memoryStream.Position = 0;
using( StreamReader streamReader = new StreamReader(memoryStream))
{
HttpContext.Current.Response.Write(streamReader.ReadToEnd());
}
}
HttpContext.Current.Response.Flush();
successful = true;
}
catch (Exception exception)
{
_logger.ErrorFormat("Unable to serialize session. Reason: {0}", exception.Message);
}
return successful;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论