【问题标题】:Save file in Web Application to local directory via a save dialog通过保存对话框将 Web 应用程序中的文件保存到本地目录
【发布时间】:2012-10-18 09:07:03
【问题描述】:

我有以下代码:

    public static string ExportToXML(DataSet dts, string Filename)
    {
        string returnmsg = "";

        try
        {

            dts.WriteXml(Filename, System.Data.XmlWriteMode.IgnoreSchema);
        }
        catch (Exception err)
        {
            returnmsg = returnmsg + err.ToString();
        }


        return returnmsg;
    }

这将帮助我将我的数据集转换为 XML 文件。我在我的网络应用程序中创建了一个按钮来调用上述函数,并期待以下屏幕:

(当然,名称不是default.aspx,而是文件名。)

我是否在我的代码中遗漏了导致单击按钮时没有出现上述对话框的任何内容?

【问题讨论】:

    标签: c# xml web-applications save


    【解决方案1】:

    您传递给WriteXml 的第一个参数,字符串Filename 是将保存在服务器上 的文件的名称。

    您需要使用HttpResponse 对象-WriteFile 方法将采用文件的路径-这可以与您在示例代码中编写的文件相同。

    所以,在你的代码后面的某个地方,你应该有如下内容:

    ExportToXML(myDataSet, theFileName);
    
    Response.WriteFile(theFileName);
    

    【讨论】:

    • 我收集了您的想法并给出了答案,并希望听到您的 cmets 的意见
    【解决方案2】:

    您应该将文件transmit 发送给客户端。

    基本上你想要

    1. 将 XML 文件保存在您的服务器上。

    2. 使用上述方法传送给客户端

    更多信息请阅读Response.TransmitFile

    【讨论】:

    • 我收集了您的想法并给出了答案,并希望听到您的 cmets 的意见。
    • 你试过了吗?它做你想做的事吗?
    【解决方案3】:

    您可以调用DataSet.WriteXml Method (Stream, XmlWriteMode) 传递Response.OutputStream 作为第一个参数。在此之前您可能需要致电Response.Clear()

    dts.WriteXml(Response.OutputStream, System.Data.XmlWriteMode.IgnoreSchema); 
    

    【讨论】:

    • 我收集了您的想法并给出了答案,并希望听到您的 cmets 的意见。
    【解决方案4】:

    在 Yener、Oded 和 Blachshma 的帮助下,

    我的代码修改如下:

        try{
                HttpContext context = HttpContext.Current;
                context.Response.Clear();
    
                //dts.WriteXml(Filename, System.Data.XmlWriteMode.IgnoreSchema);
                context.Response.Write("<?xml version=\"1.0\" standalone=\"yes\"?>");
                dts.WriteXml(context.Response.OutputStream, System.Data.XmlWriteMode.IgnoreSchema);
                context.Response.ContentType = "text/xml";
                context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Filename + ".xml");
    
                context.Response.End();
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      相关资源
      最近更新 更多