jquery fits. NET implementation Click on the specified binding data and can download a key _jquery

Source: Internet
Author: User
Tags foreach button type flush

Recently in the training management system encountered a problem, the need to click on the binding data, the specified attachment download down, and is a bulk download (binding data is not a DataGrid, background stitching binding).

The effect chart is as follows:

General ideas:

1.jquery gets the ID of the selected bound data, assigns this ID to the array, and finally assigns the value of the array to the hidden variable created in the page

2. Background gets the value of the hidden variable, and it loops the array value, get the binding value of the download address, the last package download

First in HTML, Div is bound by the background

 <div id= "Downloadinfo" runat= "Server" ></div>

The second is to download the attachment selection, using jquery implementation, and the value assigned to the page in the hidden variables, the code is as follows:

Download the attachment
  $attach = $ ("#download-list");
  var arr = []
  $attach. On (' click ', '. No ', function () {
    $ (this). Toggleclass (' checked ');/set and remove, select and uncheck
    if ( This). Hasclass (' checked ')} {
      var guid = $ (this). Children ("#hidAttachGuid"). Val ();
      Arr.push (GUID);//Add GUID to arr array
      
    }
    else
    {//unchecked
      var guid = $ (this). Children ("#hidAttachGuid"). Val ();
      var n = arr.indexof (GUID);
      if (n!=-1)
      Arr.splice (n, 1);//Remove arr array of the specified unchecked GUID
    $ ("[id$= ' ArrayGUID ']"). Val (arr);
  });

Because it is the background splicing, the button is also stitching in the background, background button calls JS

<button type= ' button ' class= ' one-download ' onclick= ' Download () ' > One-click download </button>
function Download () {
      $ ("#btnDownload"). Click ();
    

JS Triggers hidden button event

<span style= "Display:none" > <asp:button id= "btndownload" onclick= "Btndownload_click" "Text="
   determine "runat=" Server "/>
    <input type=" text id= "ArrayGUID" runat= "server"/>
</span>

Background one-click Packaging download code:

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)
      {
        //Here is the loop to get all the IDs that are specified to be downloaded
 
      }

      Download (list, "+lblcoursename.text+" Related attachment material. rar ");
    

 public void Zipfilebycode () {MemoryStream ms = new MemoryStream ();

      byte[] buffer = NULL; using (ZipFile file = Zipfile.create (ms)) {file.
        BeginUpdate (); File. Nametransform = new Mynametransfom ();//Through this name formatter, you can do some processing of the file name inside.

        By default, the relevant folder is automatically created in the zip based on the path to the file. File.
        ADD (Server.MapPath ("/content/images/img01.jpg"));

        File.commitupdate (); Buffer = new Byte[ms.
        Length]; Ms.
        Position = 0; Ms. Read (buffer, 0, buffer.)
      Length);
      } response.addheader ("Content-disposition", "Attachment;filename=test.zip");
      Response.BinaryWrite (buffer);
      Response.Flush ();

    Response.End (); }

private void Download (ienumerable<string> files, string zipfilename)
    {
      //package download based on selected file 
      MemoryStream ms = new MemoryStream ();
      byte[] buffer = null;
      using (ZipFile file = Zipfile.create (ms))
      {
        file. BeginUpdate ();
        File. Nametransform = new Mynametransfom ();//Through this name formatter, you can do some processing of the file name inside. By default, the relevant folder is automatically created in the zip based on the path to the file. 

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

and pageload the same layer code

public class MyNameTransfom:ICSharpCode.SharpZipLib.Core.INameTransform
  {

    #region Inametransform member

    public string Transformdirectory (string name)
    {return
      null;
    }

    public string TransformFile (string name)
    {return
      path.getfilename (name);
    }

    #endregion
  }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.