使用Jquery Ajax請求 下載壓縮文件


使用第三方組件:  ICSharpCode.SharpZipLib

 給按鈕綁定一個點擊事件

后台處理:

 1 public ActionResult DownZip(string ids)
 2         {
 3             if (string.IsNullOrEmpty(ids))
 4                 return Content("請選擇要操作的數據");
 5 
 6             var idArr = ids.Split(Constant.SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
 7 
 8             #region
 9             FileStream fs = null;
10             byte[] buffer = null;
11             string path = Server.MapPath("/Upload/Resource/壓縮包.zip");
12             
13             using (ZipFile file = ZipFile.Create(path))
14             {
15                 file.BeginUpdate();
16 
17                 //通過這個名稱格式化器,可以將里面的文件名進行一些處理。默認情況下,會自動根據文件的路徑在zip中創建有關的文件夾。
18                 file.NameTransform = new MyNameTransfom();                
19 
20                 foreach (var id in idArr)
21                 {
22                     var model = new BLL.APPResourceBLL().GetModel(Convert.ToInt32(id));
23                     file.Add(Server.MapPath(model.Path));
24                 }                
25 
26                 file.CommitUpdate();               
27             }
28             fs = new FileStream(path, FileMode.Open);
29             buffer = new byte[fs.Length];
30             fs.Position = 0;
31             fs.Read(buffer, 0, buffer.Length);
32             fs.Close();
33             Response.Charset = "UTF-8";
34             Response.Buffer = false;
35             Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
36             Response.ContentType = "application/octet-stream";
37             Response.AddHeader("Content-Disposition", "attachment; filename=壓縮包.zip");
38             Response.BinaryWrite(buffer);
39             Response.Flush();
40             Response.End();
41             return new EmptyResult();
42             #endregion            
43         }

前台:

 1 function downZipEvent() {
 2         if ($(".checkall:checked").size() < 1) {
 3             $.dialog.alert('請選擇下載文件!');
 4             return false;
 5         }
 6         var ids = '';
 7         $.each($(".checkall:checked"), function () {
 8             ids += $(this).attr('typeid') + ',';
 9         })
10 
11         var form = $("<form>");
12         form.attr("style", "display:none");
13         form.attr("target", "");
14         form.attr("method", "post");
15         form.attr("action", "/AppManage/AppResource/DownZip");
16         var input1 = $("<input>");
17         input1.attr("type", "hidden");
18         input1.attr("name", "ids");
19         input1.attr("value", ids);
20         $("body").append(form);
21         form.append(input1);
22         form.submit();
23         form.remove();
24     }

 

參考: http://blog.csdn.net/kongwei521/article/details/51167903

      http://www.jb51.net/article/74867.htm

      http://www.cnblogs.com/kissdodog/p/3525295.html

 

總結:當以提交表單形式發送后台,可以直接調取瀏覽器下載;

異步的方式則無法直接調用, 可以返回狀態,以表單形式在異步請求一次.

 1 $.get('/CheckBill/CompareBill', { billDate: billDate, type: type }, function (data) {
 2   var temp = data.split(':');
 3     if (temp[0] == "ok") {
 4       $('#total').text(temp[1]);
 5       $.dialog.alert('對賬成功');
 6     } else if (temp[0] == "ex") {
 7         $.dialog.alert(temp[1]);
 8         subDown(billDate);
 9     }
10     else {
11       $.dialog.alert(data);
12     }
13})

 

ajax異步加載:

https://www.cnblogs.com/wt627939556/p/6287242.html

http://www.jb51.net/article/46535.htm

https://www.cnblogs.com/fastmover/p/4791408.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM