jquery配合.NET實現點擊指定綁定資料並且能夠一鍵下載

來源:互聯網
上載者:User

標籤:return

最近在做培訓管理系統中遇到一個問題,需求需點擊繫結資料,將指定的附件下載下來,並且是批量下載(繫結資料非datagrid,後台拼接的綁定)。

如下:

650) this.width=650;" src="http://images2015.cnblogs.com/blog/640295/201610/640295-20161028105902953-1322480602.png" style="border:0px;" />

大體思路:

1.jquery得到選中的綁定資料的id,將這個id賦值到數組中,最後將這個數組的值賦值給頁面中建立的隱藏變數

2.後台擷取到隱藏變數的值,並將它迴圈數組取值,得到綁定值的,最後打包下載

  首先html中div根據後台綁定

  

 <div id="downloadInfo" runat="server"></div>

 

  

其次是下載附件的選擇,利用jquery實現,並且將值賦值給頁面中的隱藏變數,代碼如下:

 

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

// 下載附件的選擇    $attach = $("#download-list");    var arr = []    $attach.on(‘click‘, ‘.no‘, function () {        $(this).toggleClass(‘checked‘);//設定和移除,選中與不選中        if ($(this).hasClass(‘checked‘)) {            var guid = $(this).children("#hidAttachGuid").val();            arr.push(guid);//將guid添加到arr數組中                   }        else        {//取消選中時            var guid = $(this).children("#hidAttachGuid").val();            var n = arr.indexOf(guid);            if (n != -1)            arr.splice(n, 1);//將指定不選中的guid移除arr數組        }        $("[id$=‘arrayGuid‘]").val(arr);    });

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

 

因為是後台拼接的,把button也拼接在了後台,後台button 調用js

<button type=‘button‘ class=‘one-download‘ onclick=‘download()‘>一鍵下載</button>
function download() {            $("#btnDownload").click();        }

js觸發隱藏button事件

 

<span style="display: none">      <asp:Button ID="btnDownload" OnClick="btnDownload_Click" Text="確定" runat="server" />       <input type="text" id="arrayGuid" runat="server" /></span>

 

後台一鍵打包下載代碼:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

protected void btnDownload_Click(object sender, EventArgs e)        {            //ZipFileByCode();            string attachGuid = arrayGuid.Value;            string[] sArray = attachGuid.Split(‘,‘);                      List<string> list = new List<string>();            foreach (string i in sArray)            {               //這裡是迴圈得到指定需要下載的所有id             }            Download(list, ""+lblCourseName.Text+"相關附件材料.rar");        }

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

 private void Download(IEnumerable<string> files, string zipFileName)        {            //根據所選檔案打包下載              MemoryStream ms = new MemoryStream();            byte[] buffer = null;            using (ZipFile file = ZipFile.Create(ms))            {                file.BeginUpdate();                file.NameTransform = new MyNameTransfom();//通過這個名稱格式化器,可以將裡面的檔案名稱進行一些處理。預設情況下,會自動根據檔案的路徑在zip中建立有關的檔案夾。                  foreach (var item in files)                {                    file.Add(item);                }                //file.Add(Server.MapPath("../../BigFileUpLoadStorage/1.png"));                file.CommitUpdate();                buffer = new byte[ms.Length];                ms.Position = 0;                ms.Read(buffer, 0, buffer.Length);            }            Response.AddHeader("content-disposition", "attachment;filename=" + zipFileName);            Response.BinaryWrite(buffer);            Response.Flush();            Response.End();        }

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

和pageload同層代碼

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

 public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform    {        #region INameTransform 成員        public string TransformDirectory(string name)        {            return null;        }        public string TransformFile(string name)        {            return Path.GetFileName(name);        }        #endregion    }

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

添加組件:http://files.cnblogs.com/files/edisoner/ICSharpCode.SharpZipLib.rar

jquery配合.NET實現點擊指定綁定資料並且能夠一鍵下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.