有人可以解释为什么无法从 PageMethod 触发下载吗?

发布于 2024-11-30 07:15:02 字数 2064 浏览 2 评论 0原文

我有以下被称为客户端的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文