打印

[asp.net教程] 不经保存,直接读取上传文件的内容

不经保存,直接读取上传文件的内容

代码很简单,就不罗唆了。
   
  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>

TOP

返回顶部
AYBlue

Processed in 0.051442 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:31 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP