What is the best way to create a text file on a .net website?

I have a page in my vb.net web application that should throw a bunch of data into a text file and then present it to the user for download. What is the best / most efficient way to create such a text file on a .net web server?

Edit: to answer the question below, it will download once and then throw away the view of the file.

Update: I glued together the suggestions of John Rudy and DavidK, and it worked perfectly. Thank you all!

+3
source share
4 answers

The answer will depend on how the Forgotten semicolon is mentioned, you need repeated downloads or one-time throws.

, . , .

, : , - , - , () .

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ContentType = "application/octet-stream";
response.Charset = "";
response.AddHeader("Content-Disposition", String.Format("attachment; filename=\"{0}\"", filename));
response.Flush();
response.Write(text);
response.End();

.

, -, . , System.IO. , , , , IUSR_MachineName ASPNET Windows. - .

, - , , . (EG, , ? , .)

, , "-" "---", ( , , , ), , .

+3

StringBuilder , Content-Disposition.

: http://www.eggheadcafe.com/community/aspnet/17/76432/use-the-contentdispositi.aspx

private void Button1_Click(object sender, System.EventArgs e)
{
        StringBuilder output = new StringBuilder;
        //populate output with the string content
        String fileName = "textfile.txt";

        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.WriteFile(output.ToString());

}
+4

, HttpHandler :

http://digitalcolony.com/labels/HttpHandler.aspx

- , :

public void ProcessRequest(HttpContext context)
{
   response = context.Response;
   response.ContentType = "text/xml";       
   using (TextWriter textWriter = new StreamWriter(response.OutputStream, System.Text.Encoding.UTF8))
   {
       XmlTextWriter writer = new XmlTextWriter(textWriter);
       writer.Formatting = Formatting.Indented;
       writer.WriteStartDocument();
       writer.WriteStartElement("urlset");
       writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
       writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
       writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");

       // Add Home Page
       writer.WriteStartElement("url");
       writer.WriteElementString("loc", "http://example.com");
       writer.WriteElementString("changefreq", "daily");
       writer.WriteEndElement(); // url

       // Add code Loop here for page nodes
       /*
       {
           writer.WriteStartElement("url");
           writer.WriteElementString("loc", url);
           writer.WriteElementString("changefreq", "monthly");
           writer.WriteEndElement(); // url
       }
       */
       writer.WriteEndElement(); // urlset
   }                      
}
+4

Remember that it should never be a “file” on the server. This is the client that turns it into a file.

0
source

Source: https://habr.com/ru/post/1697487/


All Articles