我已經將csv文件保存爲/Exports/test.csv。我想讓用戶按下按鈕下載文件。我爲該代碼創建了一個處理程序,如下所示:在.net web應用程序中導出csv文件
<%@ WebHandler Language="C#" Class="Downloadfile" %>
using System;
using System.Web;
using System.Net;
public class Downloadfile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Clear();
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition",
"attachment; filename=" + context.Request.QueryString["file"]);
context.Response.End();
}
public bool IsReusable
{
get
{
return true;
}
}
}
上面的代碼下載該文件,但其文件爲空。我只想下載csv文件及其內容。
所以你的響應標題說響應中有一個文件,但你實際上並沒有發送文件。 – yoozer8 2013-03-11 17:38:53