读取txt文件内容,并按一定长度分页显示

private List SaveContentUpload(FileUpload file)
{
List list_content = new List();//定义集合,存储文件内容
double count = 0;
int pagesize = 1000;//设置每次读取的长度为1000
string context = "";//上传文件内容
using (Stream reader = file.PostedFile.InputStream)
{
int FileLen = file.PostedFile.ContentLength;//获取上传文件的大小
byte[] buff = new byte[FileLen];
reader.Read(buff, 0, FileLen);
reader.Position = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(reader, System.Text.Encoding.Default);
context = sr.ReadToEnd();
count = context.Length;
int num = Convert.ToInt32(Math.Ceiling(count / pagesize));
for (int i = 0; i < num; i++)
{
int sum = pagesize;
if (count - (i * pagesize) < pagesize)
{
sum = Convert.ToInt32(count - (i * pagesize));
}
string text = context.Substring(i * pagesize, sum);
list_content.Add(text);
}
}
return list_content;
}

转载于:https://www.cnblogs.com/www-mcl-1234/p/10858161.html

你可能感兴趣的:(读取txt文件内容,并按一定长度分页显示)