0

Possible Duplicate:
Write to CSV file and export it?

This is probably a silly question and I have searched on Google but I'm not able to find a definitive answer - how do you write a CSV file to the webserver and export it in C# ASP.net? I know how to generate it but I would like to save it to www.mysite.com/my.csv and then export it.

Community
  • 1
  • 1
Romulus
  • 579
  • 2
  • 9
  • 13
  • Please provide more details. Where is this CSV file generated, where do you want it to be saved and where do you want it to be exported? From what I understand from your explanation you have an ASP.NET application which is hosted on `www.mysite.com` and then it becomes unclear. – Darin Dimitrov Sep 23 '10 at 16:50
  • This is exactly the same question this user asked about 5 hours earlier, currently with 3 answers: http://stackoverflow.com/questions/3777874/write-to-csv-file-and-export-it – Wonko the Sane Sep 23 '10 at 16:51
  • None of the answers or links in that question explained how I could save a file to the virtual server, so I thought I will be more specific in a new question. – Romulus Sep 23 '10 at 16:52
  • @Romulus, what is a *virtual server*? – Darin Dimitrov Sep 23 '10 at 16:53
  • The CSV file is generated by a page in my application. I would like to save this file somewhere and export it. e.g. User goes to www.mysite.com/generatecsv.aspx, and gets the saved csv file as output. – Romulus Sep 23 '10 at 16:54
  • Closing as dupe. I'll edit your other question with the updated details (which you should have done) AND give you an answer which I believe will solve your problem. So there. –  Sep 23 '10 at 17:03

1 Answers1

1

You don't need to save the CSV file to disk especially if it is dynamically generated. You could directly write it to the response stream so that the user can download it:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "text/csv";
    Response.AppendHeader("Content-Disposition", "attachment; filename=foo.csv");
    Response.Write("val1,val2,val3");
}

You could also write an http handler:

public class CsvHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=foo.csv");
        context.Response.ContentType = "text/csv";
        context.Response.Write("val1,val2,val3");
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

which you would call like this: http://mysite.com/csvhandler.ashx

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928