protected void btNotepadPending_Click(object sender, EventArgs e) { string sFileName = System.IO.Path.GetRandomFileName(); try { String testo = "XXX" + Environment.NewLine; int iii = 0; foreach (var item in lUserPending) testo += ++iii + " - " + item.Item1 + " " + item.Item2; //Create and populate a memorystream MemoryStream mstream = GetTextAsStream(testo); //Convert the memorystream to an array of bytes. byte[] byteArray = mstream.ToArray(); //Clean up the memory stream mstream.Flush(); mstream.Close(); // Clear all content output from the buffer stream Response.Clear(); // Add a HTTP header to the output stream that specifies the default filename // for the browser's download dialog Response.AddHeader("Content-Disposition", "attachment; filename=delenda.txt"); // Add a HTTP header to the output stream that contains the // content length(File Size). This lets the browser know how much data is being transfered Response.AddHeader("Content-Length", byteArray.Length.ToString()); // Set the HTTP MIME type of the output stream Response.ContentType = "application/octet-stream"; // Write the data out to the client. Response.BinaryWrite(byteArray); Response.Flush(); Response.Close(); } catch (Exception exc) { MessageBox.SendErrorEmail("Exception: " + exc); } } private MemoryStream GetTextAsStream(String text) { //Create the return memorystream object MemoryStream ReturnStream = new MemoryStream(); //Create a streamwriter to write to the memory stream StreamWriter WriteStream = new StreamWriter(ReturnStream); //Write the text in the textbox to the Memory Stream. WriteStream.WriteLine(text); //Clean up the stream writer WriteStream.Flush(); WriteStream.Close(); //Return the memory Stream return ReturnStream; }
protected override void OnUnload(EventArgs e) { if (IsPostBack) { if (File.Exists("delenda.txt")) File.Delete("delenda.txt"); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)