1<%@ Page Language="C#" AutoEventWireup="true" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>无标题页</title>
7
8 <script runat="server">
9
10 protected void btnUpload_Click(object sender, EventArgs e)
11 {
12 if (FileUpload1.PostedFile.InputStream.Length < 1)
13 {
14 Msg.Text = "请选择文件。";
15 return;
16 }
17 string FileName = FileUpload1.FileName;
18 if (FileName.ToLower().IndexOf(".txt") == -1)
19 {
20 Msg.Text = "请选择文本文件。";
21 return;
22 }
23
24 int FileLen = FileUpload1.PostedFile.ContentLength;
25 byte[] input = new byte[FileLen];
26 System.IO.Stream UpLoadStream = FileUpload1.PostedFile.InputStream;
27 UpLoadStream.Read(input, 0, FileLen);
28 UpLoadStream.Position = 0;
29 System.IO.StreamReader sr = new System.IO.StreamReader(UpLoadStream, System.Text.Encoding.Default);
30 Msg.Text = "您上传的文件内容是:<br/><br/>" + sr.ReadToEnd();
31 sr.Close();
32 UpLoadStream.Close();
33 UpLoadStream = null;
34 sr = null;
35 }
36 </script>
37
38</head>
39<body>
40 <form id="MengXianhui" runat="server">
41 <div>
42 <asp:FileUpload ID="FileUpload1" runat="server" />
43 <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click"></asp:Button>
44 <asp:Label ID="Msg" runat="server" ForeColor="Red"></asp:Label>
45 </div>
46 </form>
47</body>
48</html>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>无标题页</title>
7
8 <script runat="server">
9
10 protected void btnUpload_Click(object sender, EventArgs e)
11 {
12 if (FileUpload1.PostedFile.InputStream.Length < 1)
13 {
14 Msg.Text = "请选择文件。";
15 return;
16 }
17 string FileName = FileUpload1.FileName;
18 if (FileName.ToLower().IndexOf(".txt") == -1)
19 {
20 Msg.Text = "请选择文本文件。";
21 return;
22 }
23
24 int FileLen = FileUpload1.PostedFile.ContentLength;
25 byte[] input = new byte[FileLen];
26 System.IO.Stream UpLoadStream = FileUpload1.PostedFile.InputStream;
27 UpLoadStream.Read(input, 0, FileLen);
28 UpLoadStream.Position = 0;
29 System.IO.StreamReader sr = new System.IO.StreamReader(UpLoadStream, System.Text.Encoding.Default);
30 Msg.Text = "您上传的文件内容是:<br/><br/>" + sr.ReadToEnd();
31 sr.Close();
32 UpLoadStream.Close();
33 UpLoadStream = null;
34 sr = null;
35 }
36 </script>
37
38</head>
39<body>
40 <form id="MengXianhui" runat="server">
41 <div>
42 <asp:FileUpload ID="FileUpload1" runat="server" />
43 <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click"></asp:Button>
44 <asp:Label ID="Msg" runat="server" ForeColor="Red"></asp:Label>
45 </div>
46 </form>
47</body>
48</html>