ASP. net for txt file-related operations (read, write, save), asp. nettxt

Source: Internet
Author: User

ASP. net for txt file-related operations (read, write, save), asp. nettxt

ASP. NET reads the 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; // obtain the namespace test {public partial class Text: System. web. UI. page {protected void Page_Load (object sender, Event Args e) {Response. write (GetInterIDList ("asp.txt");} // read the 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 ;}}}

Reading the txt file is to get the file stream. Remember to reference using System. IO ;.

ASP. NET ):

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 row sw. writeLine ("ASP. NET network programming-helping customers! "); Sw. Close ();

Note: If you do not need to wrap the text in notepad, you can use Write. If you need to wrap the text, you can use WriteLine.

ASP. NET Save the txt file (Notepad ):

Public void ProcessRequest (HttpContext context) {context. response. clear (); context. response. buffer = true; // Server. urlEncode prevents garbled context of the saved file name. response. addHeader ("Content-Disposition", "attachment; filename =" + context. server. urlEncode ("Consumption details" + string. format ("{0: yyyyMMddHHmmss}", System. dateTime. now) + ". txt "); context. response. contentType = "text/plain"; string message = "Hello World"; // use Environment to wrap the exported file. newLine message + = "Hello World" + Environment. newLine; context. response. write (message); // stop the execution context on the page. response. end ();}

NOTE 3:

1. Saving the file name garbled: Use Server. UrlEncode for encoding

2.txt: Environment. NewLine

3. js: window. location. href = "download. ashx" or window. open ("download. ashx ")

The above is aboutTxt fileIf my article is helpful to you, just give it a compliment.

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.