In ASP.NET, how to get the browser to download string content into a file? (C#)

前端 未结 6 420
情书的邮戳
情书的邮戳 2021-01-13 06:27

I would like to create a text file for export/download, like a *.csv, from an ASP.NET application. I know about Response.TransmitFile, but I want to do this without creating

6条回答
  •  悲哀的现实
    2021-01-13 07:05

    When you say "Create a file for export", I am understanding that you want to make it downloadable to the browser. If that's the case, here's an example.

    public void btnGo_Click (Object sender, EventArgs e)
    {
        Response.Clear();
    
        string fileName= String.Format("data-{0}.csv", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
        Response.ContentType = "text/csv";
        Response.AddHeader("content-disposition", "filename=" + fileName);
    
        // write string data to Response.OutputStream here
        Response.Write("aaa,bbb,ccc\n");
    
        Response.End();
    }
    

    cite: RFC 4180

提交回复
热议问题