Public voidProcessRequest (HttpContext context) {context. Response.Clear (); Context. Response.Buffer=true; //server.urlencode prevent garbled file names from being savedContext. Response.AddHeader ("content-disposition","attachment;filename="+ context. Server.URLEncode ("Consumption details"+string. Format ("{0:YYYYMMDDHHMMSS}", System.DateTime.Now) +". txt")); Context. Response.ContentType="Text/plain"; stringMessage ="Hello World"; //If the exported file is to be wrapped, use the Environment.NewLineMessage + ="Hello World"+Environment.NewLine; Context. Response.Write (message); //Stop the execution of a pagecontext. Response.End (); }
Note two points:
1. Save file name garbled problem: encode with Server.URLEncode
Line break in 2.txt file: Environment.NewLine
3. Call can be used js:window.location.href= "Download.ashx" or window.open ("Download.ashx")