0

我只想能够按一个按钮并获取要下载的 xmldocument 文件。

我已经尝试了很多事情,例如:

XmlDocument doc As XmlDocument() = //Method that gets a xmldocument
Response.Clear()
this.Response.ContentType = "text/xml"
xmldoc.Save(this.Response.OutputStream)

Dim xmldocument As XmlDocument = //Method that gets a xmldocument
Using stream As MemoryStream = New MemoryStream()
            Dim xmlWriter As XmlTextWriter = New XmlTextWriter(stream, System.Text.Encoding.ASCII)
            xmldocument.WriteTo(xmlWriter)
            xmlWriter.Flush()

            Dim byteArray As Byte() = stream.ToArray()
            Response.Clear()
            Response.AppendHeader("Content-Disposition", "filename=MyExportedFile.xml")
            Response.AppendHeader("Content-Length", byteArray.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Response.BinaryWrite(byteArray)
            xmlWriter.Close()
        End Using

没有任何效果,我是否忘记了一些明显的事情?因为没有任何效果,所以加载了 xmldocument 一切似乎都很好,但文件从不下载,“响应”完全没有任何作用!

Csharp 或 VB.NET 中的答案会有所帮助

4

2 回答 2

2

尝试这样的事情

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"
<root name='rootAttribute'>
    <OrderRequest name='one' />
    <OrderRequest name='two' />
    <OrderRequest name='three' />
</root>
"); // Load some random xml - use function to load whatever you need

MemoryStream ms = new MemoryStream();
using (XmlWriter writer = XmlWriter.Create(ms))
{
    doc.WriteTo(writer); // Write to memorystream
}

byte[] data = ms.ToArray();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "text/xml";
HttpContext.Current.Response.AddHeader("Content-Disposition:",
                    "attachment;filename=" + HttpUtility.UrlEncode("samplefile.xml")); // Replace with name here
HttpContext.Current.Response.BinaryWrite(data);
HttpContext.Current.Response.End();
ms.Flush(); // Probably not needed
ms.Close();
于 2012-12-05T16:10:21.990 回答
0

你有没有尝试过...

Response.ContentType = "text/xml"
Response.Write(MethodThatReturnsXMLDoc().innerXml)
于 2012-12-05T15:57:13.560 回答