简体   繁体   English

如何即时生成 xml 文件并使用 ASP.NET 内核将其下载到请求浏览器

[英]How to generate xml file on the fly and download it to the requesting browser using ASP.NET Core

I'm trying to generate XML file on the fly and download it to the requesting browser (client's machine) in ASP.NET Core 5.0.我正在尝试动态生成 XML 文件并将其下载到 ASP.NET Core 5.0 中的请求浏览器(客户端计算机)。

I tried this one with no luck:我试过这个没有运气:

public Task GenerateXML() 
{
    // here I just created the XmlDocument
    XmlDocument doc = new XmlDocument();

    // and then created the xml declaration, root element and sub elements
    ..........
    // here are the codes to export the XML file to the requesting browser
    MemoryStream stream = new MemoryStream();
    XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);

    doc.WriteTo(writer);
    writer.Flush();
    Response.Clear();

    byte[] byteArray = stream.ToArray();
    Response.Headers.Append("Content-Disposition", "filename=Books.xml");
    Response.Headers.Append("Content-Length", byteArray.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.Body.Write(byteArray);
    writer.Close();
}

I tried to use your code to accomplish this, in fact, your code is working and the file can be downloaded, but the download prompt box does not appear.我试过用你的代码来完成这个,其实你的代码是可以的,可以下载文件,但是没有出现下载提示框。

When you open your browser's download list, the file should appear in your list.当您打开浏览器的下载列表时,该文件应出现在您的列表中。

在此处输入图像描述

I use JavaScript to call this method, the download prompt box appears normally and the file can be downloaded.我用JavaScript调用这个方法,正常出现下载提示框,可以下载文件了。

Below is my test code,you can refer to it:下面是我的测试代码,大家可以参考:

Controller: Controller:

public void GenerateXML()
{
            XmlDocument doc = new XmlDocument();

            // XML declaration
            XmlNode declaration = doc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
            doc.AppendChild(declaration);

            // Root element: Catalog
            XmlElement root = doc.CreateElement("Catalog");
            doc.AppendChild(root);

            // Sub-element: srsapiversion of root
            XmlElement book = doc.CreateElement("book");
            root.AppendChild(book);


            // Sub-element: srsapiversion of root
            XmlElement bookname = doc.CreateElement("bookname");
            book.InnerText = "2 States";
            root.AppendChild(bookname);

            // Sub-element: id of root
            XmlElement id = doc.CreateElement("id");
            id.InnerText = "70-515";
            root.AppendChild(id);

            // Sub-element: author of root
            XmlElement author = doc.CreateElement("author");
            author.InnerText = "Chetan Bhagat";
            //Attribute age of author
            XmlAttribute age = doc.CreateAttribute("age");
            age.Value = "43";
            author.Attributes.Append(age);
            root.AppendChild(author);

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, System.Text.Encoding.UTF8);

            doc.WriteTo(writer);
            writer.Flush();
            Response.Clear();
            byte[] byteArray = stream.ToArray();
            Response.Headers.Add("Content-Disposition", "filename=Books.xml");
            Response.Headers.Add("Content-Length", byteArray.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Body.Write(byteArray);
            writer.Close();
}

JavaScript: JavaScript:

<script>
    window.open('/Home/GenerateXML', 'DownloadWindowName');
</script>

Test Result:测试结果:

在此处输入图像描述 在此处输入图像描述

Hope this will help you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM