【发布时间】:2025-11-21 21:45:01
【问题描述】:
我已经将 csv 文件保存为 /Exports/test.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 文件及其内容。
【问题讨论】:
-
所以您的响应标头说响应中有一个文件,但您实际上并没有发送该文件。
标签: c# .net export-to-csv