读取txt文件内容,并按一定长度分页显示
private list<string> savecontentupload(fileupload file)
{
list<string> list_content = new list<string>();//定义集合,存储文件内容
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;
}