简体   繁体   English

为什么我没有使用c#在我的asp.net应用程序中获取.csv文件?

[英]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.

相关问题 为什么我在使用C#的asp.net中的密码字段中获取旧密码? - why I am getting old password in a password field in asp.net using C#? 为什么我在 ASP.NET Core 6 C# 中收到此错误? - Why am I getting this error in ASP.NET Core 6 C#? 我无法从Excel文件中读取并使用C#中的asp.net存储到数据库中? - I am not able to read from Excel file and store into database using asp.net in c#? 我尝试在 *somee.com* 上托管一个 ASP.NET C# web 应用程序,但我无法正确显示我的主页 - I tried hosting an ASP.NET C# web application on *somee.com* and I am not able to display my homepage correctly 使用C#asp.net无法获得正确的文件路径 - Not getting proper file path using c# asp.net 我如何在c#asp.net应用程序中使用jquery实现文本框自动建议? - How can i implement textbox auto suggest using jquery in my c# asp.net application? 一直在尝试使用 C# 在 ASP.NET MVC 中将迁移添加到我的项目中,但出现这些错误 - Have been trying to add migration to my project in ASP.NET MVC using C# but am getting these errors 我正在尝试在 asp.net c# 中使用 google API 发送 email 并收到错误 400 redirect_uri_mismatch - I am trying to send email using google API in asp.net c# and getting Error 400 redirect_uri_mismatch 我在ASP.NET应用程序中收到空指针异常 - I am getting null pointer exception in asp.net application 我在 ASP.Net Webform 应用程序上遇到配置错误 - I am getting configuration error on ASP.Net Webform application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM