asp.net fileupload上传控件直接读取上传文件方法

发布时间:2020-09-30编辑:脚本学堂
有关asp.net fileupload上传控件上传文件时,不经保存直接上传文件的实例代码,asp.net fileupload上传控件入门例子,需要的朋友参考下。

asp.net fileupload上传控件上传文件专题教程:asp.net fileupload控件上传文件教程大全),代码:
 

复制代码 代码示例:
<%@ page language="c#" autoeventwireup="true" codebehind="testmemcache.aspx.cs" inherits="linq_to_sql.testmemcache" enableviewstate="false"%>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>asp.net fileupload上传文件_www.jb200.com</title>
</head>
<body>
<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>
</body>
</html>

后台.cs文件:
 

复制代码 代码示例:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;

namespace linq_to_sql
{
public partial class testmemcache : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}

protected void btnupload_click(object sender, eventargs e)
{
if (getuploadfilecontent.postedfile.inputstream.length < 1)
{
msg.text = "请选择文件";
return;
}
string filename = getuploadfilecontent.filename;//上传文件文件名
string filepath = getuploadfilecontent.postedfile.filename;//上传文件完整路径+文件名
if (filename.tolower().indexof(".htm") == -1)
{
msg.text = "请选择要求的类型的文件";
return;
}

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;
}
}
}