Using third-party components: Icsharpcode.sharpziplib
Bind a Click event to a button
Background processing:
1 PublicActionResult Downzip (stringIDs)2 {3 if(string. IsNullOrEmpty (IDS))4 returnContent ("Please select the data you want to manipulate");5 6 varIdarr =IDs. Split (Constant.SEPARATOR.ToCharArray (), stringsplitoptions.removeemptyentries);7 8 #region9FileStream fs =NULL;Ten byte[] buffer =NULL; One stringPath = Server.MapPath ("/upload/resource/compressed package. zip"); A - using(ZipFile file =zipfile.create (path)) - { the file. BeginUpdate (); - - //With this name formatter, you can do some processing of the file name inside. By default, the relevant folder is created automatically based on the path of the file in the zip. -File. Nametransform =Newmynametransfom (); + - foreach(varIdinchIdarr) + { A varModel =NewBLL. APPRESOURCEBLL (). Getmodel (Convert.ToInt32 (ID)); at file. ADD (Server.MapPath (model. Path)); - } - - file.commitupdate (); - } -FS =NewFileStream (path, filemode.open); inBuffer =New byte[FS. Length]; -Fs. Position =0; toFs. Read (Buffer,0, buffer. Length); + FS. Close (); -Response.Charset ="UTF-8"; theResponse.Buffer =false; *response.contentencoding = System.Text.Encoding.GetEncoding ("UTF-8"); $Response.ContentType ="Application/octet-stream";Panax NotoginsengResponse.AddHeader ("content-disposition","attachment; filename= compressed package. zip"); - response.binarywrite (buffer); the Response.Flush (); + Response.End (); A return NewEmptyresult (); the #endregion +}
Front desk:
1 functiondownzipevent () {2 if($ (". checkall:checked"). Size () < 1) {3$.dialog.alert (' Please select Download file! ‘);4 return false;5 }6 varids = ";7$.each ($ (". checkall:checked"),function () {8IDS + = $ ( This). attr (' typeid ') + ', ';9 })Ten One varForm = $ ("<form>"); AForm.attr ("Style", "Display:none"); -Form.attr ("Target", "" "); -Form.attr ("Method", "POST"); theForm.attr ("Action", "/appmanage/appresource/downzip"); - varINPUT1 = $ ("<input>"); -Input1.attr ("type", "hidden"); -Input1.attr ("name", "IDs"); +Input1.attr ("Value", IDS); -$ ("Body"). append (form); + form.append (INPUT1); A form.submit (); at Form.remove (); -}
Reference: http://blog.csdn.net/kongwei521/article/details/51167903
Http://www.jb51.net/article/74867.htm
Http://www.cnblogs.com/kissdodog/p/3525295.html
Download compressed files using jquery Ajax requests