程序集: System.Web(在 system.web.dll 中)
实例:
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.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+ "上传文件成功";
}
}
}
实例效果图:
上传完成: