写入数据到TXT文件:
HTML页面:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>写入TXT</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function () {
//提交信息
$("#btnAppointment").click(function () {
var username = $("#username").val();
var grade = $("#grade").val();
var phone = $("#phone").val();
if ($.trim(username) == '' || $.trim(grade) == '' || $.trim(phone) == '') {
alert("请填写完所有的信息,再提交预约!谢谢!");
return;
}
username = username.replace("|", "").replace(";", "");
grade = grade.replace("|", "").replace(";", "");
phone = phone.replace("|", "").replace(";", "");
var myDate = new Date();
var datetime = myDate.toLocaleString();
var info = username + "|" + grade + "|" + phone + "|" + datetime + ";";
//alert(info);
$.get("WriteTxt.ashx?info=" + info,
function (result) {
if (result == "1") {
alert("预约成功!");
window.location.reload();
}
else {
alert("预约失败,错误代码:" + result);
return;
}
});
});
});
</script>
</head>
<body>
<form class="form-signin" role="form">
<input type="text" maxlength="5" id="username" placeholder="姓名"><br />
<input type="text" maxlength="5" id="grade" placeholder="年级"><br />
<input type="text" maxlength="11" id="phone" placeholder="电话"><br />
<input type="button" id="btnAppointment" value="预约试听" /><br /><br />
<a href="list.aspx" target="_blank">预约列表</a>
</form>
</body>
</html>
一般处理程序页面:
<%@ WebHandler Language="C#" Class="WriteTxt" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
public class WriteTxt : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string info = context.Request.QueryString["info"];//url参数
const string strfile = "info.txt";//txt文件名
string txtPath = HttpContext.Current.Server.MapPath(strfile);//相对路径转绝对路径
string strout = string.Empty;//txt文件里读出来的内容
try
{
if (!string.IsNullOrEmpty(info))
{
if (!File.Exists(txtPath))
{
//文件不存在....
context.Response.Write("-1");//返回key:文件不存在(-1)
}
else//文件存在,读取....
{
FileStream myStream = new FileStream(txtPath, FileMode.Append, FileAccess.Write);// FileMode.Append,追加一行数据
StreamWriter sw = new StreamWriter(myStream);
sw.WriteLine(info);//写入文件
sw.Close();
context.Response.Write("1");//返回key:写入成功(1)
}
}
else
{
context.Response.Write("0");//返回key:写入失败(0)
}
}
catch
{
context.Response.Write("0");//返回key:写入失败(0)
}
}
public bool IsReusable {
get {
return false;
}
}
}
读取TXT文件数据:
HTML页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="list.aspx.cs" Inherits="list" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>读取TXT</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1">
<thead>
<tr>
<th>姓名</th>
<th>年级</th>
<th>电话</th>
<th>时间</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td><%#Eval("username") %></td>
<td><%#Eval("grade") %></td>
<td><%#Eval("phone") %></td>
<td><%#Eval("datetime") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
</form>
</body>
</html>
后台处理页面:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
public partial class list : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
const string strfile = "info.txt";//txt文件名
string txtPath = HttpContext.Current.Server.MapPath(strfile);//相对路径转绝对路径
string strout = string.Empty;//txt文件里读出来的内容
try
{
//string txt = File.ReadAllText(txtPath, Encoding.Default);//如果txt内容较少可用此法
if (File.Exists(txtPath))
{
using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(strfile), System.Text.Encoding.Default))
{
strout = sr.ReadToEnd();
string temp = strout.Replace("\r\n", "");
string[] strArr = temp.Split(';');//分解每一组数据
if (strArr != null && strArr.Any())
{
List<Info> list = new List<Info>();
foreach (var item in strArr)
{
if (!string.IsNullOrEmpty(item) && !string.IsNullOrWhiteSpace(item))
{
string[] strArr2 = item.Split('|');//分解每一个属性
Info info = new Info();
info.username = strArr2[0].ToString();
info.grade = strArr2[1].ToString();
info.phone = strArr2[2].ToString();
info.datetime = DateTime.Parse(strArr2[3].ToString());
list.Add(info);
}
}
if (list != null && list.Any())
{
this.Repeater1.DataSource = list.OrderByDescending(a=>a.datetime);
this.Repeater1.DataBind();
}
}
}
}
}
catch (Exception ex)
{
var ddd = ex.ToString();
}
}
public class Info
{
public string username { get; set; }
public string grade { get; set; }
public string phone { get; set; }
public DateTime datetime { get; set; }
}
}
配套源码下载地址:https://download.csdn.net/download/djk8888/10466397