解决方法
实现IHttpHandler.
我在ProcessResponse中使用类似于以下的东西来输出之前在数据库表中构造的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以使其写入那里而不是文件流.