代碼很簡單,就不羅唆了。
C#:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<script runat="server">
protected void btnUpload_Click( object sender, EventArgs e )
{
if (FileUpload1.PostedFile.InputStream.Length < 1)
{
Msg.Text = "請選擇文件。";
return;
}
string FileName = FileUpload1.FileName;
if (FileName.ToLower().IndexOf(".txt") == -1)
{
Msg.Text = "請選擇文本文件。";
return;
}
int FileLen = FileUpload1.PostedFile.ContentLength;
byte[] input = new byte[FileLen];
System.IO.Stream UpLoadStream = FileUpload1.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 = "您上傳的文件內容是:<br/><br/>" + sr.ReadToEnd();
sr.Close();
UpLoadStream.Close();
UpLoadStream = null;
sr = null;
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml">
<head runat="server">
<title>不經保存,直接讀取上傳文件的內容</title>
</head>
<body>
<form id="MengXianhui" runat="server">
<ASP:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="上傳" OnClick="btnUpload_Click"></ASP:Button>
<p>
<asp:Label ID="Msg" runat="server" ForeColor="Red"></ASP:Label></p>
</form>
</body>
</Html>