我只是希望能够按下按钮并获取要下载的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中的答案会有所帮助
答案 0 :(得分: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();
答案 1 :(得分:0)
你试过......
Response.ContentType = "text/xml"
Response.Write(MethodThatReturnsXMLDoc().innerXml)