aspx前台页面代码
<form id="form1" action="File.aspx" runat="server" enctype="multipart/form-data"> <div> <input type="file" id="filename" name="filename" value="" /><input type="submit" value="上传" /> </div> </form
后台页面代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 8 namespace FileUpload 9 { 10 public partial class File : System.Web.UI.Page 11 { 12 protected void Page_Load(object sender, EventArgs e) 13 { 14 if (IsPostBack) 15 { 16 HttpPostedFile hp = Request.Files["filename"]; 17 string filename = hp.FileName; 18 string[] filenum = filename.Split('.'); 19 string[] newnum = filenum[0].Split(':'); 20 string[] newnums = newnum[newnum.Length - 1].Split('\\'); 21 string filextention = filenum[filenum.Length - 1]; 22 23 string newFilename = newnums[newnums.Length - 1] + DateTime.Now.Year + DateTime.Now.Month.ToString() + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + "." + filextention; 24 string path =Request.MapPath("/Files/") + newFilename; 25 hp.SaveAs(path); 26 27 } 28 } 29 } 30 }