asp.Netでは添付ファイルの一括アップロードとダウンロードを実現します.

71819 ワード

実は私のところには一度に10個の添付ファイルしかアップロードできません.js制御を通じてです.(別のasp.netの一括アップロード:http://www.cnblogs.com/52net/archive/2012/06/14/2549174.html)
アップロード画面:添付ファイルの追加をクリックし、添付ファイルを追加した後、後ろの続行をクリックして添付ファイルを追加することができます.
asp.net 中实现附件批量上传与下载。_第1张图片
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新しくページを開きます.一つ選択すればダウンロードできます.
 
asp.net 中实现附件批量上传与下载。_第2张图片
 
 
添付ファイルダウンロードページの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" />
    
        
    
        &nbsp;&nbsp;
    
        
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="  " />
         &nbsp;<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>");
    }
}