注意一定要加runat=“Server”,否则无法执行服务器端代码
页面代码:
<form id="form1" runat="server"> <input id="uploadfile" type="file" size="9" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label">文件:</asp:Label> </form>
Button1_Click代码:
protected void Button1_Click(object sender, EventArgs e) { DateTime now = System.DateTime.Now; string strBaseLocation = "d:\\up\\"; string filename = now.DayOfYear.ToString() + uploadfile.PostedFile.ContentLength.ToString() + uploadfile.PostedFile.FileName; //上传文件名 uploadfile.PostedFile.SaveAs(strBaseLocation+filename); Label1.Text = "上传成功,文件名为:" + filename; }