Reputation: 1
I've got an issue with a simple web application I'm developing in C# using VS 2013. It's supposed to take some values from a grid view and export them to a .csv file, however when I test, it's just taking that information and displaying it as text on the page. I wrote a test page with a single link button and tried to export a values to a text file but I get the same result.
protected void LinkButton1_Click(object sender, EventArgs e)
{
string test = "1,1,1,,,";
StringBuilder sb = new StringBuilder();
sb.AppendLine(test);
Response.Clear();
Response.ContentType = "text/plain";
Response.AddHeader("content-disposition","attachement; test.txt");
Response.Write(sb.ToString());
Response.Flush();
Response.End();
}
The code looks correct (I've compared it to several examples) but I still don't know why I'm not being prompted to save or open the file. I've tried it in multiple browsers but I get the same result in each one. Aside of adding line number Visual Studio is setup with default options. Could there be a problem with VS? Or IIS? Or even my workstation?
Upvotes: 0
Views: 90
Reputation: 1
I think it was the: Response.AddHeader("Content-Dispostion","attachment; filename=test.txt")
That corrected the problem in test page so that it was prompting to save the text file. Then I changed the Content Type to text/csv and tried again and again it prompted to save a csv file. So I copied that code an used it the page that was giving me the problem to begin with an that too is prompting to a csv file. Thanks for all the input everyone.
Upvotes: 0
Reputation: 4835
Try
Response.AddHeader("Content-Disposition","attachment; filename=test.txt");
Upvotes: 1