Asp. NET to TXT file related operation (read, write, Save) _ Practical skills

Source: Internet
Author: User
Tags httpcontext save file urlencode

ASP. NET read TXT file (Notepad) content:

 using System; 
Using System.Collections; 
Using System.Configuration; 
Using System.Data; 
Using System.Web; 
Using System.Web.Security; 
Using System.Web.UI; 
Using System.Web.UI.HtmlControls; 
Using System.Web.UI.WebControls; 
Using System.Web.UI.WebControls.WebParts; 
Using System.IO; Get TXT file stream namespace test {public partial class Text:System.Web.UI.Page {protected void Page_Load (object send 
  Er, EventArgs e) {Response.Write (Getinteridlist ("Asp.txt")); 
   //Read TXT file content public string getinteridlist (String strfile) {string strout; 
   Strout = ""; if (! File.exists (System.Web.HttpContext.Current.Server.MapPath (strfile))) {} else {StreamReader sr = new 
    StreamReader (System.Web.HttpContext.Current.Server.MapPath (strfile), System.Text.Encoding.Default); String input = Sr. 
    ReadToEnd (); Sr. 
    Close (); 
   Strout = input; 
  return strout; } 
 } 
}

Read TXT file content is to get the file stream, remember to reference using System.IO;.

Asp. NET Write TXT file (Notepad):

String txtpath = Server.MapPath ("~\\public\\attinfo\\") + "Test.txt"; 
StreamWriter sw = new StreamWriter (Txtpath, False, System.Text.Encoding.Default); 
Sw. WriteLine ("Hello World"); 
Sw. WriteLine (""); Output empty line 
SW. WriteLine ("ASP. NET programming-cloud-dwelling community! "); 
Sw. Close ();

Note: If write Notepad does not need to wrap, you can use write, you need to wrap, you can use WriteLine.

Asp. NET Save txt file (Notepad):

public void ProcessRequest (HttpContext context)
  {context

   . Response.Clear ();
   Context. Response.Buffer = true;
   Server.URLEncode prevents saved file names from garbled context
   . Response.AddHeader ("Content-disposition", "attachment;filename=" + context. Server.URLEncode ("Consumption detail" + string.) Format ("{0:YYYYMMDDHHMMSS}", System.DateTime.Now) + ". txt"); 
   Context. Response.ContentType = "Text/plain"; 
   String message = "Hello World";
   If the exported file is to be wrapped, use environment.newline message
   = + "Hello World" + Environment.NewLine;
   Context. Response.Write (message);
   Stops the execution context of the page  
   . Response.End (); 
  }

Note 3 points:

1. Save file name garbled problem: using Server.URLEncode encoding

Line wrapping in 2.txt file: Environment.NewLine

3. Calls can be js:window.location.href= "Download.ashx" or window.open ("Download.ashx")

The above is about TXT file related operation, if my article is helpful to you, just order a praise.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.