3

I am trying to make a csv file from a textbox and then send it to the user. This is my code so far:

Response.Clear();
            Response.ContentType = "text/csv";
            Response.AppendHeader("Content-Disposition",
                string.Format("attachment; filename={0}", DateTime.Now));

            Response.Write(TextBox_Data.Text);
            Context.Response.End();

What is sent is an empty xml file, I have never tried responding with a file before and I'm wondering why it does this?

I have also tried the following which did not work:

var writer = File.CreateText("C:\\file.csv");
            writer.WriteLine(TextBox_Data.Text);

            Context.Response.Clear();
            Context.Response.AppendHeader("content-disposition", "attachment; filename=" + DateTime.Now + ".csv");
            Context.Response.ContentType = "text/csv";
            Context.Response.Write("C:\\file.csv");
            Context.Response.Flush();
            Context.Response.End();

Let me know if you have the answer :)

5
  • You are trying to create a CSV file from what a user puts in a text box, and then email it to the user, or have the user automatically download it? Can you give us a little more context for the how and why beyond this code? Commented Sep 14, 2011 at 14:00
  • Try writing to a different path. Perhaps to your profile path. Writing to your root is dangerous. Commented Sep 14, 2011 at 14:09
  • Indeed, I am trying to make a downloadable CSV file from a multiline textbox :) Actually I don't care about putting the file on my drive, I just want a downloadable CSV file, with the text from the textbox. Commented Sep 14, 2011 at 14:30
  • Writing to the root is not only dangerous, but unless you have explicitly given permission to your app pool identity to do so, it will fail for security reasons Commented Sep 14, 2011 at 15:12
  • Another sidenote is that the tostring() of datetime creates a string with a space. The space makes the file lose it's type (also loses the hours and minutes of the datetime. Commented Sep 17, 2011 at 8:44

2 Answers 2

7

The following code worked for me. You may just be missing a file extension.

Response.Clear();
Response.ContentType = "text/csv";
Response.AppendHeader("Content-Disposition",
                string.Format("attachment; filename={0}.csv", DateTime.Now));
Response.Write(TextBox_Data.Text);
Context.Response.End();
Sign up to request clarification or add additional context in comments.

2 Comments

Keep in mind this code will not take care of things like escape characters but I assume that's what you're expecting.
ahh... I wrote the extension like this ("attachment; filename={0}", DateTime.Now+".csv")); This works perfectly from my home pc, Thanks a lot!
2

Just a complement on joshb's answer regarding the use of Response.End():

MSDN does not recommend the use of Response.End() for non-error cases, and in some cases it can actually cause the client to lose some data .

In my case sometimes the downloaded csv would loose the last bytes of the last line, so I removed the Response.End() and used

    HttpContext.Current.ApplicationInstance.CompleteRequest()

instead, and I had to override the page's Render(HtmlTextWriter writer) method to not write anything on the request, since the csv was already writen.

public class PageBase : Page
{
    private bool performRegularPageRender = true;

    protected override void Render(HtmlTextWriter writer)
    {
        if (performRegularPageRender)
            base.Render(writer);
    }

    public void SkipRegularPageRendering()
    {
        performRegularPageRender = false;
    }
}

More info / credits: msdn blog; Is Response.End() considered harmful?; PostBack and Render Solutions? Overrides

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.