msi
msi

Reputation:

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

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 and saving a file physically on the server. Is that possible? Has anyone done something like that?

Upvotes: 5

Views: 11322

Answers (6)

Noldorin
Noldorin

Reputation: 147280

You'll want to look at writing a Custom HTTP Handler (a class that implements IHttpHandler) and simply register it in web.config. See this article on MSDN for a good example of how to set one up.

Here's a basic example of how you might go about implementing one to return the markup for some CSV data.

using System.Web;

public class MyCsvDocumentHandler : IHttpHandler
{
    public static string Data
    {
        get;
        set;
    }

    public MyCsvDocumentHandler()
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/csv"; // Set the MIME type.
        context.Response.Write(Data); // Write the CSV data to the respone stream.
    }

    public bool IsReusable
    {
        // To enable pooling, return true here.
        // This keeps the handler in memory.
        get { return false; }
    }
}

This alternative, which is possibly slightly simpler, is to use an ASHX handler page. The code would be almost identical.

Upvotes: 4

Cheeso
Cheeso

Reputation: 192467

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

Upvotes: 13

NinethSense
NinethSense

Reputation: 9028

Try this sample:

protected void Button1_Click(object sender, EventArgs e)
{
     Response.ContentType = "text/csv";
     Response.AddHeader("content-disposition", "attachment; filename=download.csv");
     Response.Write("your,csv,file,contents");
     Response.End();
}

Upvotes: 0

JasonRShaver
JasonRShaver

Reputation: 4394

Oh, that is not bad. In your ASPX page's Page_Load do this:

Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(/* your text goes here */);

The above is an example if your 'file' is xml, but it can be anything, from and excel file to a pdf. All you have to do is update the ContentType which you can lookup via Google or Live.

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827316

You could write direcly to the Response.OutputStream and set the right content type, and content disposition header.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415725

A file you haven't saved yet is just a string variable or a MemoryStream. But for large amounts of data you probably don't want to keep it all in memory. What do you want to do with this "file" once you have it?

Upvotes: 1

Related Questions