zoukankan      html  css  js  c++  java
  • jquery配合.NET实现点击指定绑定数据并且能够一键下载

    最近在做培训管理系统中遇到一个问题,需求需点击绑定的数据,将指定的附件下载下来,并且是批量下载(绑定的数据非datagrid,后台拼接的绑定)。

    效果图如下:

    大体思路:

    1.jquery得到选中的绑定数据的id,将这个id赋值到数组中,最后将这个数组的值赋值给页面中创建的隐藏变量

    2.后台获取到隐藏变量的值,并将它循环数组取值,得到绑定值的下载地址,最后打包下载

      首先html中div根据后台绑定

      

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

      

    其次是下载附件的选择,利用jquery实现,并且将值赋值给页面中的隐藏变量,代码如下:

    复制代码

    // 下载附件的选择
    $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);
    });

    复制代码

    因为是后台拼接的,把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>

    后台一键打包下载代码:

    复制代码

    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");
    }

    复制代码

    复制代码

     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();
    }

    复制代码

    和pageload同层代码

    复制代码

     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
    }

    复制代码

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

  • 相关阅读:
    httpcontext in asp.net unit test
    initialize or clean up your unittest within .net unit test
    Load a script file in sencha, supports both asynchronous and synchronous approaches
    classes system in sencha touch
    ASP.NET MVC got 405 error on HTTP DELETE request
    how to run demo city bars using sencha architect
    sencha touch mvc
    sencha touch json store
    sencha touch jsonp
    51Nod 1344:走格子(贪心)
  • 原文地址:https://www.cnblogs.com/zhaohongtian/p/6802347.html
Copyright © 2011-2022 走看看