Posted: May 15th, 2009 | Filed under: ASP.NET | Tags: File | Comments Off
To save you all (as well as my future self) the trouble of searching all over the place just to find terrible answers on almost every form post out there. Here is a small ASP.NET/C# code snippet that will prompt the user with the save/open dialog box to download a file.
String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();
For more content types check our http://en.wikipedia.org/wiki/MIME_type#List_of_common_media_types
Posted: February 14th, 2009 | Filed under: ASP.NET, C Sharp | Tags: ASP.NET | Comments Off
In asp.net input taken from a plain text box will not display newline characters (Hitting the enter key) when outputting to the browser.
So for example you have a text box that users can input text into, they put a couple paragraphs in it and submit that data to the database. If you just strait output the content to the browser you will see one giant blob of text. So how can you fix this?
By converting the newline to a html
tag we can fix the problem. So with a little c# function we have the trick done.
public String FixLabelOutput(String val) {
return val.Replace(Environment.NewLine, "<br />");
}
As you see our function simple returns all newlines as <br /> which will fix the way it is outputted.
In the aspx page you need to have this to display the corrected text. This of course can be placed in a label or just strait printed.
<%# FormatHtml(Eval("TextInfo").ToString()) %>