操作:
在页面内放置一个FileUpload控件 ,然后放置上传的Button 或者其他控件.
在 Button或者其他控件的 click 事件中 写上传的方法.
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upfile.aspx.cs" Inherits="web_upfile" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" Height="20"
onclick="Button1_Click" />
</div>
</form>
</body>
</html>
后台:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class web_upfile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //click protected void Button1_Click(object sender, EventArgs e) { string fileName = FileUpload1.FileName; //获得文件名 string fileType = fileName.Substring(fileName.LastIndexOf('.') + 1);//获得文件类型 string serverPath = "";//获取存放目录 double size = FileUpload1.PostedFile.ContentLength; if (size > 6000) { Response.Write("<script>alert('您选择的文件过大!')</script>"); } else { //保存文件 serverPath = Server.MapPath("../upload/")+fileName; FileUpload1.SaveAs(serverPath); } } }
如果要配置上传文件的大小,请参考:http://www.cnblogs.com/ajing2008/archive/2007/06/05/771764.html
感谢大侠,感谢CCTV.