我已經在SQL Server 2005的存儲過程生成一個170兆字節的XML文件(使用SELECT FOR XML
)如何流從SQL Server 2005中的XML HTTP響應流下載
如何在ASP.NET,我會將此XML流式傳輸下載爲網頁瀏覽器中的文件?
我已經在SQL Server 2005的存儲過程生成一個170兆字節的XML文件(使用SELECT FOR XML
)如何流從SQL Server 2005中的XML HTTP響應流下載
如何在ASP.NET,我會將此XML流式傳輸下載爲網頁瀏覽器中的文件?
像這樣的事情已經工作了我一會兒。注意你必須調整使其適合於您的情況:
var settings = new XmlWriterSettings {
// your settings
};
using (var xmlWriter = XmlWriter.Create(response.Output, settings))
using (var connection = YourConnection())
using (var command = CreateCommand(sql, connection, dbParams) as SqlCommand)
{
try {
connection.Open();
using (var reader = command.ExecuteXmlReader())
{
while (reader.Read())
xmlWriter.WriteNode(reader, true);
}
}
catch (Exception ex)
{
// log, whatever
throw;
}
}
另外,如果你想下載的反應,你必須設置HTTP頭,像this。這實際上是一個單獨的問題。
我同意@Metropolitan,您的輸出大小對於網絡下載來說可疑的大。我只是試圖回答這個問題,但你可能不得不重新考慮這個問題。
在此之後應該插入一些行中的一些表,這個文件已經準備就緒。
事情是這樣的......
反正170 MB XML hmmmm聽起來像是你要檢討許多事情以減少輸出大小...
Harpo的答案很好,但您需要準備好您的響應以使其在ASP.NET中工作。創建一個頁面,你可以把它稱爲「Download.aspx」。然後在Page_load中,您需要傳輸內容。
protected void Page_Load(object sender, EventArgs e)
{
try
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
if (Request.QueryString["key"] != null)
{
Response.AppendHeader("content", contentType);
Response.AppendHeader("content-disposition", "attachment; filename=something.xml");
Response.ContentType = "text/xml";
// Here you would call your sproc (see harpo's answer)
// and write to Response.Output
}
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}
catch (Exception ex)
{
// Handle your errors
}
}
我只從'IHttpHandler'做這種事情,所以不需要明確的方法。但好點。 – harpo 2013-05-11 02:08:45
啊,是的,這是有效的。 – Tombala 2013-05-11 03:06:49