protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Session["demo"] = new List<String>(); } } private static int f;//给全局变量并赋值0,static可以保存值 protected void Button1_Click(object sender, EventArgs e) { string FileName = FileUpload2.PostedFile.FileName; if (FileName == "") { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), string.Format("<script>alert('上传文件不能为空!')</script>")); } else { string file_KB = FileUpload2.PostedFile.ContentLength.ToString() + "KB<br>";//获取文件大小 string file = FileUpload2.FileName;//获取上传文件名字 string URL = "http://amid01110/456/" + file + " "; List<string> list = Session["demo"] as List<string>; Session["demo"] = list; string lntext = "<a href='" + URL + "' >" + file + "</a>" + "大小" + file_KB + "上传文件成功"; Label1.Text += Label1.Text.Length > 0 ? "<br />" + lntext : lntext; f++;//添加的个数 Label2.Text = f.ToString(); } }
前台
<div> <asp:FileUpload ID="FileUpload2" runat="server" /> <asp:Button ID="Button3" runat="server" Text="Button" OnClick="Button1_Click" /> 共上传<asp:Label ID="Label2" runat="server"></asp:Label> 个<br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </div>
后台:方法二
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ViewState["demo"] = 0; } } protected void Button1_Click(object sender, EventArgs e) { string FileName = FileUpload2.PostedFile.FileName; if (FileName == "") { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(), string.Format("<script>alert('上传文件不能为空!')</script>")); } else { string file_KB = FileUpload2.PostedFile.ContentLength.ToString() + "KB<br>";//获取文件大小 string file = FileUpload2.FileName;//获取上传文件名字 ViewState["demo"] = Convert.ToInt32(ViewState["demo"]) + 1;//ViewState["demo"]获取个数 string URL = "http://amid01110/456/" + file + " "; string lntext = "<a href='" + URL + "' >" + file + "</a>" + "大小" + file_KB + "上传文件成功"; Label1.Text += Label1.Text.Length > 0 ? "<br />" + lntext : lntext; Label2.Text = ViewState["demo"].ToString(); } }