2009-12-10 63 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

相關問題