1. 程式人生 > >ASP.NET FileUpload讀取上傳檔案的內容

ASP.NET FileUpload讀取上傳檔案的內容

好久沒更了,來一個

前臺:

<form id="MengXianhui" runat="server">
    <asp:FileUpload ID="GetUploadFileContent" 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>
後臺cs:
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 = "您上傳的檔案內容是:<br/><br/>" + sr.ReadToEnd();
sr.Close();
UpLoadStream.Close();
UpLoadStream = null;
sr = null;