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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM