Jquery works with. NET to achieve click to specify data binding and one-click Download, jquery.net

Source: Internet
Author: User

Jquery works with. NET to achieve click to specify data binding and one-click Download, jquery.net

Recently, I encountered a problem in the training management system. I need to click the bound data to download the specified attachment and batch download (the bound data is not a datagrid, backend stitching ).

As follows:

General idea:

1. jquery gets the id of the selected bound data, assigns this id to the array, and finally assigns the value of this array to the Hidden variable created on the page.

2. The background obtains the value of the Hidden variable, loops through the array to obtain the bound value, and finally downloads the package.

First, bind the div in html to the background

  

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

 

  

The second step is to download the attachment selection, which is implemented using jquery and assign the value to the Hidden variable on the page. The Code is as follows:

 

// Select $ attach = $ ("# download-list") for downloading attachments; var arr = [] $ attach. on ('click ','. no ', function () {$ (this ). toggleClass ('checked'); // set and remove, select or unselect if ($ (this ). hasClass ('checked') {var guid = $ (this ). children ("# hidAttachGuid "). val (); arr. push (guid); // Add the guid to the arr array} else {// var guid =$ (this) when the selected guid is canceled ). children ("# hidAttachGuid "). val (); var n = arr. indexOf (guid); if (n! =-1) arr. splice (n, 1); // remove the arr array from the specified unselected guid} $ ("[id $ = 'arraygu']"). val (arr );});

 

Because it is spliced in the background, the button is also spliced in the background, and the background button calls js

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

Js triggers hidden button events

 

<Span style = "display: none"> <asp: button ID = "btnDownload" OnClick = "btnDownload_Click" Text = "OK" runat = "server"/> <input type = "text" id = "arrayGuid" runat = "server" /> </span>

 

One-click packaging and downloading code in the background:

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 to be downloaded} Download (list, "" + lblCourseName. text + "related accessories .rar ");}
Public void ZipFileByCode () {MemoryStream MS = new MemoryStream (); byte [] buffer = null; using (ZipFile file = ZipFile. create (MS) {file. beginUpdate (); file. nameTransform = new MyNameTransfom (); // you can use this name formatter to process the file name. By default, related folders are automatically created in the zip file based on the file path. File. add (Server. mapPath ("/Content/images/im1_1.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) {// Download MemoryStream MS = new MemoryStream (); byte [] buffer = null according to the selected file package; using (ZipFile file = ZipFile. create (MS) {file. beginUpdate (); file. nameTransform = new MyNameTransfom (); // you can use this name formatter to process the file name. By default, related folders are automatically created in the zip file based on the file path. 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 ();}

Code at the same layer as pageload

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}

Add component: http://files.cnblogs.com/files/edisoner/ICSharpCode.SharpZipLib.rar

 

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.