前台代码:
<asp:Label ID="Label5" runat="server" Text="上传图片:"></asp:Label> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
后台代码:
if (FileUpload1.HasFile) { string Name = FileUpload1.FileName; //文档上传到的固定目录 如果需要修改必须必须3个都修改 string SaveSoft = Server.MapPath("~\\UploadFiles\\"); string Fe = Name.Substring(Name.LastIndexOf(".") + 1); string newName = ""; if (Name != "") { newName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "." + Fe; } SaveSoft += newName; System.IO.Directory.CreateDirectory(Server.MapPath("~\\UploadFiles\\")); FileUpload1.SaveAs(SaveSoft); txtPic.Text = "UploadFiles\\" + newName; }