程序集: System.Web(在 system.web.dll 中)
实例:
html代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="inputfile._Default" %>
<!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>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
// ]]>
</script>
<style type="text/css">
.style1
{
text-align: center;
}
#form1
{
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div style="position:static;">
<div class="style1">
演示文件上传控件
</div>
<hr style="width:80%" />
<div class="style1">
<asp:FileUpload ID="File1" runat="server"/>
<asp:Button ID="UploadBtn" runat="server" onclick="Button1_Click" Text="上传" />
</div>
</div>
<asp:Label ID="Label1" runat="server"
Width="437px" Height="61px"></asp:Label>
</form>
</body>
</html>
<!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>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
// ]]>
</script>
<style type="text/css">
.style1
{
text-align: center;
}
#form1
{
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div style="position:static;">
<div class="style1">
演示文件上传控件
</div>
<hr style="width:80%" />
<div class="style1">
<asp:FileUpload ID="File1" runat="server"/>
<asp:Button ID="UploadBtn" runat="server" onclick="Button1_Click" Text="上传" />
</div>
</div>
<asp:Label ID="Label1" runat="server"
Width="437px" Height="61px"></asp:Label>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace inputfile
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{//获取文件信息
string FileName = File1.PostedFile.FileName;
string file_str = "文件名称:" + FileName + "<br>";
file_str="文件类型:"+File1.PostedFile.ContentType+"<br>";
file_str="文件长度:"+File1.PostedFile.ContentLength.ToString()+"KB<br>";
//上传文件到服务器
FileName = FileName.Substring(FileName.LastIndexOf("\\") + 1);//取出文件名的路径(不包括文件的名称)
string upload_file = Server.MapPath("./upload/") + FileName;//取出服务器虚拟路径,存储上传文件
File1.PostedFile.SaveAs(upload_file);//开始上传文件
Label1.Text =file_str+ "上传文件成功";
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace inputfile
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{//获取文件信息
string FileName = File1.PostedFile.FileName;
string file_str = "文件名称:" + FileName + "<br>";
file_str="文件类型:"+File1.PostedFile.ContentType+"<br>";
file_str="文件长度:"+File1.PostedFile.ContentLength.ToString()+"KB<br>";
//上传文件到服务器
FileName = FileName.Substring(FileName.LastIndexOf("\\") + 1);//取出文件名的路径(不包括文件的名称)
string upload_file = Server.MapPath("./upload/") + FileName;//取出服务器虚拟路径,存储上传文件
File1.PostedFile.SaveAs(upload_file);//开始上传文件
Label1.Text =file_str+ "上传文件成功";
}
}
}
常用属性:
(1)FileUpload1.HasFile用来检查 FileUpload是否有指定文件。
(2)HttpContext.Current.Request.MapPath("~/") 则是获取网站所在的磁盘绝对路径的,如D:\Inetpub\ServerControls\路径,之所以要这么做,是因为FileUpload控件必须 指定“绝对路径”,而非相对路径,同时绝对路径也必须有写入权限。
(3)FileUpload1.SaveAs()则是将上传文件 存储在磁盘的方法。
(4)FileUpload1.FileName用于获取上传文件名称。
(5)FileUpload1.PostedFile.ContentLength 用于设置或获取上传文件大小,以Byte为单位。
(6)FileUpload1.PostedFile.ContentType 用于设置或获取上传文件的类型实例效果图:
上传完成:
本文转自shenzhoulong 51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/303131,如需转载请自行联系原作者