Compressed file-related classes:
Public classZipcompressutil { Public Statictuple<BOOL, Stream> Zip (stringStrziptopdirectorypath,intIntziplevel,stringStrpassword,string[] filesordirectoriespaths) { Try{List<string> Allfilespath =Newlist<string>(); if(Filesordirectoriespaths.length >0)//Get all Files path { for(inti =0; i < filesordirectoriespaths.length; i++) { if(File.exists (Filesordirectoriespaths[i])) {Allfilespath.add (Fileso Rdirectoriespaths[i]); } Else if(Directory.Exists (Filesordirectoriespaths[i])) {Getdirectoryfiles ( Filesordirectoriespaths[i], Allfilespath); } } } if(Allfilespath.count >0) {MemoryStream ms=NewMemoryStream (); Zipoutputstream Zipoutputstream=NewZipoutputstream (MS); Zipoutputstream.setlevel (Intziplevel); Zipoutputstream.password=strpassword; for(inti =0; i < Allfilespath.count; i++) { stringstrfile =Allfilespath[i]; Try { if(Directory.Exists (strfile))//folder { stringstrFileName = Strfile.replace (Strziptopdirectorypath,""); if(Strfilename.startswith ("") ) {strFileName= Strfilename.substring (0); } ZipEntry Entry=NewZipEntry (strFileName +"/"); Entry. DateTime=DateTime.Now; Zipoutputstream.putnextentry (entry); } Else //file{FileStream fs=File.openread (strfile); byte[] buffer =New byte[FS. Length]; Fs. Read (Buffer,0, buffer. Length); stringstrFileName = Strfile.replace (Strziptopdirectorypath,""); if(Strfilename.startswith ("") ) {strFileName= Strfilename.substring (0); } ZipEntry Entry=NewZipEntry (strFileName); Entry. DateTime=DateTime.Now; Zipoutputstream.putnextentry (entry); Zipoutputstream.write (Buffer,0, buffer. Length); Fs. Close (); Fs. Dispose (); } } Catch { Continue; }} zipoutputstream.finish (); return Newtuple<BOOL, Stream> (true, MS); } Else { return Newtuple<BOOL, Stream> (false,NULL); } } Catch { return Newtuple<BOOL, Stream> (false,NULL); } } Private Static voidGetdirectoryfiles (stringStrparentdirectorypath, list<string>Allfilespath) { string[] files =Directory.GetFiles (Strparentdirectorypath); for(inti =0; I < files. Length; i++) {Allfilespath.add (files[i]); } string[] Directorys =directory.getdirectories (Strparentdirectorypath); for(inti =0; I < Directorys. Length; i++) {getdirectoryfiles (directorys[i], allfilespath); } if(Files. Length = =0&& Directorys. Length = =0)//Empty Folder{allfilespath.add (Strparentdirectorypath); } } }
Invoke and provide a method for downloading
PublicActionResult Export () {List<Long> productids =NULL; varURL =Request.rawurl; if(URL.) Contains ("?")) { stringParamstr = URL. Substring (URL. LastIndexOf ("?")); string[] ParamArray = Paramstr.split ('&'); Productids=ParamArray. Select (Item= = Item. Split ('='). Length >1? Item. Split ('=')[1] :"0") . Select (Value= = value. Trylong (0)). ToList (); } if(Directory.Exists (Server.MapPath ("~/Listed Products")) {directory.delete (Server.MapPath ("~/Listed Products"),true); } varProductList =ProductOnSaleService.Instance.GetProductSkuImgsExportList (productids); foreach(varProductiteminchproductlist) { vardir = Server.MapPath ("~/listed Products/") + Productitem.productid +"-"+Productitem.longname; if(!directory.exists (dir)) {directory.createdirectory (dir); } foreach(varSkuiteminchProductitem.skus) {Bitmap Rqbtimap=Qrcodehelper.encode (BizKeyValService.Instance.Get (Enumbizkey.productshareurl). Bizval+"? id="+ Productitem.productid +"&skuid="+ Skuitem.skuid,430,430); Rqbtimap.save (dir+"/"+ Productitem.productid +"-"+ Skuitem.skuid +"-"+ Skuitem.skuname +". jpg"); } } varStrziptopdirectorypath = Server.MapPath ("~/"); Const intIntziplevel =6; Const stringstrpassword =""; varFilesordirectoriespaths =New string[] {Server.MapPath ("~/Listed Products") }; varresult =Zipcompressutil.zip (Strziptopdirectorypath, Intziplevel, strpassword, filesordirectoriespaths); varBuffer =New byte[result. Item2.length]; Result. Item2.position=0; Result. Item2.read (Buffer,0, buffer. Length); Result. Item2.close (); Response.appendheader ("content-disposition","attachment;filename= shelves products. zip"); Response.BinaryWrite (buffer); Response.Flush (); Response.End (); return NewEmptyresult (); }
JS Code
$ ("#btnExpress"). Click (function () { varSelectedlist = $.getselectid (true); if(Selectedlist.length = = 0) {$.alert ("Please select Product"); return; } varurl = "/product/productonsale/export"; for(vari = 0; i < selectedlist.length; i++) {URL= (i = = 0? url + "? param" + i + "=" +selectedlist[i].tostring (): URL+ "¶m" + i + "=" +selectedlist[i].tostring ()); } location.href=URL; });
ASP. NET MVC file compression download