实际上我这里一次性只能上传10个附件,就是通过js控制。
上传界面:点击添加附件,添加完一个附件后,点击后面的继续可以继续添加附件。
HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="附件上传.aspx.cs" Inherits="wsw_测试_附件上传与下载_附件上传" %> <!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> <script language="javascript" type="text/javascript"> // <!CDATA[ function Button17_onclick() { document.getElementById("uplTable").style.display = ""; } function Button_add_fj_onclick() { document.getElementById("TABLE_D0").style.display = ""; document.getElementById("Button_add_fj").style.display = "none"; document.getElementById("TABLE_D1").style.display = ""; } function Button_D1_onclick() { document.getElementById("TABLE_D2").style.display = ""; document.getElementById("Button_D1").style.display = "none"; } function Button_D2_onclick() { document.getElementById("TABLE_D3").style.display = ""; document.getElementById("Button_D2").style.display = "none"; } function Button_D3_onclick() { document.getElementById("TABLE_D4").style.display = ""; document.getElementById("Button_D3").style.display = "none"; } function Button_D4_onclick() { document.getElementById("TABLE_D5").style.display = ""; document.getElementById("Button_D4").style.display = "none"; } function Button_D5_onclick() { document.getElementById("TABLE_D6").style.display = ""; document.getElementById("Button_D5").style.display = "none"; } function Button_D6_onclick() { document.getElementById("TABLE_D7").style.display = ""; document.getElementById("Button_D6").style.display = "none"; } function Button_D7_onclick() { document.getElementById("TABLE_D8").style.display = ""; document.getElementById("Button_D7").style.display = "none"; } function Button_D8_onclick() { document.getElementById("TABLE_D9").style.display = ""; document.getElementById("Button_D8").style.display = "none"; } function Button_D9_onclick() { document.getElementById("TABLE_D10").style.display = ""; document.getElementById("Button_D9").style.display = "none"; } function Button1_onclick() { divv0.style.display = "none"; } </script> </head> <body> <form id="form1" runat="server"> <div> <table class="table_bt" cellpadding="2" cellspacing="1"> <tr> <td> <input id="Button_add_fj" οnclick="return Button_add_fj_onclick()" type="button" value="添加附件" class="btn_big" height="20px" width="60px" /> <asp:Button ID ="btn_wc" runat="server" Text="上传" class="btn_big" Height="20px" Width="60px" οnclick="btn_wc_Click" /> <asp:Button ID ="btn_xz" runat="server" Text="附件下载" class="btn_big" Height="20px" Width="60px" οnclick="btn_xz_Click" /> </td> </tr> </table> <table width="100%"> <tr> <td> <table style="padding: 0px; margin: 0px;" id="table_sc" visible="true" runat="server"> <tr id="uplTable"> <td width="80%"> <table id="TABLE_D0" width="100%"> <tr visible="false"> <td> <table id="TABLE_D1" style="display: none"> <tr align="left"> <td align="left"> <input id="FileUploadD1" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D1" cssclass="a_search" οnclick="return Button_D1_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D2" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD2" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D2" cssclass="a_search" οnclick="return Button_D2_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D3" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD3" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D3" cssclass="a_search" οnclick="return Button_D3_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D4" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD4" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D4" cssclass="btn_fy" οnclick="return Button_D4_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D5" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD5" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D5" cssclass="btn_fy" οnclick="return Button_D5_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D6" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD6" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D6" cssclass="a_search" οnclick="return Button_D6_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td class="style1"> <table id="TABLE_D7" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD7" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D7" cssclass="a_search" οnclick="return Button_D7_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D8" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD8" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D8" cssclass="a_search" οnclick="return Button_D8_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D9" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD9" type="file" size="40" cssclass="a_search" runat="server" /> <input id="Button_D9" cssclass="a_search" οnclick="return Button_D9_onclick()" type="button" value="继续" /> </td> </tr> </table> </td> </tr> <tr visible="false"> <td> <table id="TABLE_D10" style="display: none"> <tr align="center"> <td align="left"> <input id="FileUploadD10" cssclass="a_search" type="file" size="40" runat="server" /> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </div> </form> </body> </html>
后台操作代码:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; public partial class wsw_测试_附件上传与下载_附件上传 : System.Web.UI.Page { CX.DBGetResult dbgr = new CX.DBGetResult(CX.AppInfo.DBMS, CX.AppInfo.ConStr);//数据库连接字符串 protected void Page_Load(object sender, EventArgs e) { } protected void btn_wc_Click(object sender, EventArgs e) { HtmlInputFile fu1; //上传控件 fu1 = this.FileUploadD1; //调用上传方法 ViewState["wjm"] = UpLoadFile(fu1);//获取附件存储的根目录。 //上传控件 fu1 = this.FileUploadD2; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD3; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD4; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD5; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD6; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD7; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD8; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD9; //调用上传方法 UpLoadFile(fu1); //上传控件 fu1 = this.FileUploadD10; //调用上传方法 UpLoadFile(fu1); string SQL = "INSERT INTO fj (DZ) VALUES ('" + ViewState["wjm"].ToString() + "')"; this.dbgr.ExecSQL(SQL); } /// <summary> /// 文件上传。 /// </summary> /// <param name="InputFile"></param> /// <param name="Path"></param> public string UpLoadFile(HtmlInputFile InputFile) { //判断有控件内容进入 string b = ""; string xdd = ""; try { xdd = InputFile.PostedFile.FileName.ToString();//获取上传文件的名字 } catch (Exception e) { return xdd; } if (xdd != "") { string wjm = DateTime.Now.ToString("yyyyMMddhhmmss"); string path = "~/FileUploadCS/" + wjm + "/"; string PRICE_FJ_FJMC = ""; string PRICE_FJ_FJHZ = ""; string PRICE_FJ_PATH_SC = ""; string PRICE_FJ_PATH_XZ = ""; PRICE_FJ_PATH_XZ = path; //上传全路径包括文件名G:\\新建文件夹 (4)\\新建 文本文档.txt PRICE_FJ_PATH_SC = InputFile.PostedFile.FileName; //文件名包括后缀 新建 文本文档.txt" PRICE_FJ_FJMC = PRICE_FJ_PATH_SC.Substring(PRICE_FJ_PATH_SC.LastIndexOf("\\") + 1); //建立上传对象 // HttpPostedFile postedFile = InputFile.PostedFile; //文件名包括后缀 新建 文本文档.txt" string fileName = System.IO.Path.GetFileName(postedFile.FileName); //文件后缀 PRICE_FJ_FJHZ = System.IO.Path.GetExtension(fileName); string phyPath = HttpContext.Current.Request.MapPath(PRICE_FJ_PATH_XZ);//获取文件所在根目录 //判断路径是否存在,若不存在则创建路径 DirectoryInfo upDir = new DirectoryInfo(phyPath); if (!upDir.Exists) { upDir.Create(); } // //保存文件 // try { postedFile.SaveAs(phyPath + PRICE_FJ_FJMC);//文件保存 return b = phyPath; } catch { string a = ""; throw new ApplicationException("上传失败!"); return b = a; } //返回上传文件的信息 } return b; } protected void btn_xz_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(ViewState["wjm"].ToString())) { string wjm = ViewState["wjm"].ToString();//获取文件夹的名字 wjm = Server.UrlEncode(wjm);//要进行编码的转换 Page.RegisterStartupScript("", " <script language=javascript> window.open ( '附件下载.aspx?wjm=" + wjm + " ','下载界面', 'height=300, width=400, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no ') </script> "); } } }
当我点击附件下载后 就会跳到附件下载的界面,我这里是用window。open新打开页。 选中一个就可以下载了。
附件下载页的HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="附件下载.aspx.cs" Inherits="wsw_测试_附件上传与下载_附件下载" %> <!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 id="Head1" runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <%--<asp:ListBox runat ="server" ID="listBox1"></asp:ListBox>--%> <asp:RadioButtonList runat ="server" ID="ck" /> <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="下载" /> <asp:Button ID="Button2" runat="server" Text="关闭" οnclick="Button2_Click" /> </div> </form> </body> </html>
后台:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; public partial class wsw_测试_附件上传与下载_附件下载 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string path = Server.UrlDecode(Request.QueryString["wjm"].ToString());//获取路径WJM ViewState["path"] = path; DirectoryInfo TheFolder = new DirectoryInfo(path); DirectoryInfo[] dirInfo = TheFolder.GetDirectories(); //遍历文件夹 //foreach (DirectoryInfo NextFolder in dirInfo) // this.listBox1.Items.Add(NextFolder.Name); FileInfo[] fileInfo = TheFolder.GetFiles(); //遍历文件 foreach (FileInfo NextFile in fileInfo) this.ck.Items.Add(NextFile.Name); } } protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < ck.Items.Count; i++) { if (ck.Items[i].Selected == true) { string path = ViewState["path"].ToString() + ck.Items[i].Text; FileInfo file = new System.IO.FileInfo(path); Page.Response.Buffer = false; Page.Response.Charset = "UTF-8"; Page.Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode((file.Name))); Page.Response.HeaderEncoding = System.Text.Encoding.UTF8; Page.Response.ContentEncoding = System.Text.Encoding.UTF8;//GetEncoding("UTF-8"); Page.Response.ContentType = "application/ms-excel"; Page.Response.WriteFile(file.FullName); //System.IO.File.Delete(file.FullName); Page.Response.End(); } } } protected void Button2_Click(object sender, EventArgs e) { Response.Write("<script>window.close()</script>"); } }
DEMO下载地址:https://dwz.cn/fgXtRtnu