2009-06-03 51 views

回答

1

我創建了一個StringBuilder,並使用以下代碼將內容轉儲到Response對象(「csv」是StringBuilder變量)。

Response.ContentType = @"application/x-msdownload"; 
    Response.AppendHeader("content-disposition", "attachment; filename=" + FILE_NAME); 

    Response.Write(csv.ToString()); 
    Response.Flush(); 
    Response.End(); 
1

使用context.Response.OutputStream。

Here's一個例子。

2

實現一個IHttpHandler。

我用類似於ProcessResponse爲outputing之前已經在一個數據庫表構建一個CSV以下的東西......

public void ProcessRequest(HttpContext context) 
{ 
    HttpResponse response = context.Response; 
    HttpRequest request = context.Request; 

    //Get data to output here... 

    //Turn off Caching and enforce a content type that will prompt to download/save. 
    response.AddHeader("Connection", "close"); 
    response.AddHeader("Cache-Control", "private"); 
    response.ContentType = "application/octect-stream"; 

    //Give the browser a hint at the name of the file. 
    response.AddHeader("content-disposition", string.Format("attachment; filename={0}", _filename)); 

    //Output the CSV here... 
    foreach(BatchDTO.BatchRecordsRow row in dtoBatch.BatchRecords) 
     response.Output.WriteLine(row.Data); 

    response.Flush(); 
    response.Close(); 
} 

有許多,使生成CSV容易庫,你應該能夠將它傳遞給Response.OutputStream讓它寫到那裏,而不是文件流。

+0

我發現如果你有很多數據你也應該添加response.BufferOutput = false; ,以便立即彈出文件下載框,而不是等待所有行在服務器上緩衝。 – 2009-10-30 03:39:01

相關問題