zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    First Missing Positive
    Find Minimum in Rotated Sorted Array II
    switch两种写法对比
    常用的前端JavaScript方法封装
    如何保证缓存和数据库的一致性?
    14个前端小知识
    dataTable转换特定的类
    C# MD5 32大写位加密 UTF-8编码
    另一个 SqlParameterCollection 中已包含 SqlParameter
    C#实现数据回滚,A事件和B事件同时执行,其中任何一个事件执行失败,都会返回失败
  • 原文地址:https://www.cnblogs.com/moy-1313133/p/7977818.html
Copyright © 2011-2022 走看看