實際上我這里一次性只能上傳10個附件,就是通過js控制。 (另一個asp.net的批量上傳:http://www.cnblogs.com/52net/archive/2012/06/14/2549174.html)
上傳界面:點擊添加附件,添加完一個附件后,點擊后面的繼續可以繼續添加附件。
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" onclick="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" onclick="btn_wc_Click" /> <asp:Button ID ="btn_xz" runat="server" Text="附件下載" class="btn_big" Height="20px" Width="60px" onclick="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" onclick="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" onclick="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" onclick="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" onclick="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" onclick="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" onclick="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" onclick="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" onclick="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" onclick="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" onclick="Button1_Click" Text="下載" /> <asp:Button ID="Button2" runat="server" Text="關閉" onclick="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>"); } }