讀取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;
}string

相關文章
相關標籤/搜索