2009-12-10 42 views
0

我有一个大型的Web表单应用程序,我想允许用户导出他们的数据,以防他们不想一次完成整个表单。然后,当他们返回时,他们可以导入数据并继续他们离开的地方。在不保存到磁盘的情况下传输生成的XML文件

原因是客户端要求的一部分是不使用数据库。

我已经到了创建包含所有表单数据的XML文件的地步,但是我希望客户端能够下载该XML文件,而无需应用程序将其保存到服务器,即使是暂时的。

是否可以创建XML文件并通过流/附件将其传输到客户端而不将其保存到磁盘?

我正在使用C#Asp.net

回答

2

您可以写信给HttpResponse对象:

 HttpResponse response = HttpContext.Current.Response; 

     string xmlString = "<xml>blah</xml>"; 
     string fileName = "ExportedForm.xml"; 

     response.StatusCode = 200; 

     response.AddHeader("content-disposition", "attachment; filename=" + fileName); 
     response.AddHeader("Content-Transfer-Encoding", "binary"); 
     response.AddHeader("Content-Length", _Buffer.Length.ToString()); 

     response.ContentType = "application-download"; 
     response.Write(xmlString); 
+0

请注意,我专门使用ContentType为“application-download”而不是更精确的MIME类型的“application/xml”或“text/xml”。这是为了解决IE6中“content-disposition”标题中的文件名未用于默认“另存为”文件名的问题。 – 2009-12-10 19:35:25

+0

工作就像一个魅力。谢谢! – TruthStands 2009-12-10 19:35:30

0

是的。在PHP它看起来像这样:

header("Content-type: text/xml"); 
$headerText = "Content-disposition: attachment; filename=file.xml"; 
header($headerText); 
echo $your_XML_contents; 
exit; 
+0

哎呀,我忘了提我使用C#Asp.net – TruthStands 2009-12-10 19:06:29

+0

然后更加注重mlsteeves'评论,同样的事情,但他是语言不可知论者:D – tloach 2009-12-10 19:11:41

1
HttpResponse response = HttpContext.Current.Response; 

    string xmlString = "<xml>blah</xml>"; 
    string fileName = "ExportedForm.xml"; 

    response.StatusCode = 200; 

    response.AddHeader("content-disposition", "attachment; 

filename =“+ fileName); response.AddHeader(“Content-Transfer-Encoding”,“binary”); response.AddHeader(“Content-Length”, _Buffer.Length.ToString());

response.ContentType = "application-download"; 
    response.Write(xmlString); 

但它保存所有网页内容到文件

+1

我解决了这个问题。但是现在我遇到了国家标志的问题。如何在响应中设置编码? – Alexander 2011-09-30 08:25:31

+1

也解决了它。响应。ContentEncoding = System.Text.Encoding.GetEncoding(“windows-1257”); – Alexander 2011-09-30 09:30:01

相关问题