asp.net 上傳XML,txt 直接讀取文件內容

if (GetUploadFileContent.PostedFile.InputStream.Length < 1)
{
Msg.Text = "請選擇文件";
return;
}
string FileName = GetUploadFileContent.FileName;//上傳文件文件名
string FilePath = GetUploadFileContent.PostedFile.FileName;//上傳文件完整路徑+文件名
string fileExtName = FileName.Substring(FileName.LastIndexOf(".") + 1, 3);
if (fileExtName == "txt" || fileExtName == "TXT" || fileExtName == "xml" || fileExtName == "XML")
{
int FileLen = GetUploadFileContent.PostedFile.ContentLength;//獲取上傳文件的大小
byte[] input = new byte[FileLen];
System.IO.Stream UpLoadStream = GetUploadFileContent.PostedFile.InputStream;
UpLoadStream.Read(input, 0, FileLen);
UpLoadStream.Position = 0;
System.IO.StreamReader sr = new System.IO.StreamReader(UpLoadStream, System.Text.Encoding.Default);
Msg.Text = sr.ReadToEnd();
if (Msg.Text != "")
{
 //此處的Msg.Text 就是文本中文件內容!
}
}
else
{
Msg.Text = "文件爲空!";
return;
}
sr.Close();
UpLoadStream.Close();
UpLoadStream = null;
sr = null;
}
else
{
Msg.Text = "請選擇要求的類型的文件!";
return;
}xml

相關文章
相關標籤/搜索