[英]why i am not getting the .csv file in my asp.net application using c#?
i am trying to get a .csv file on client machine by a click of a button by this code: 我试图通过此代码单击按钮在客户端计算机上获取.csv文件:
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "text/csv"; ;
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.AddHeader("Content-Disposition", "attachment; filename=WBS.csv");
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
string output = "WBSCode,Description,TerritoryCode,Amount" + "\n";
Response.Write(output);
the problem is the code is executing but i am not getting a .csv file in my machine.. what is the problem ? 问题是代码正在执行但我没有在我的机器中获得.csv文件..问题是什么? i am doing this for the 1st time 我是第一次这样做
You need to create a generic handler (.ashx file), which will send the csv as response. 您需要创建一个通用处理程序(.ashx文件),它将csv作为响应发送。 Your button then simply needs to have a url to that .ashx. 然后你的按钮只需要有一个.ashx的网址。 Below is the example of the handler with some of your code. 下面是一些代码的处理程序示例。
public class GetCsvFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/csv";
context.Response.AddHeader("Content-Disposition", "attachment; filename=WBS.csv");
context.Response.Write("WBSCode,Description,TerritoryCode,Amount\n");
}
public bool IsReusable
{
get
{
return true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.