• ajax下载多文件,并且打包 C#中 ,文件批下载zip


    //提交要下载的文件

    $.ajax({
    url:"/sub/ZipFile.aspx",
    data:"paras="+datas,
    type: 'HEAD',
    error: function () {
    alert("压缩包不存在!");
    },
    success: function () {
    window.location.href = "/sub/ZipFile.aspx?paras=" + datas;
    window.close;
    }
    });

    //接收文件并调用进行压缩

    public string paras = "";//要下载的文件路径
    MyNameTransfom mt = new MyNameTransfom();
    protected void Page_Load(object sender, EventArgs e)
    {
    paras = Request.QueryString["paras"];//要下载的文件名称
    if (paras.Length > 0)
    {
    string[] aa = paras.Split(',');
    mt.ZipFileDownload(aa, DateTime.Now.ToString("yyyyMMddhhMmss") + "_17cn.zip", HttpContext.Current);
    }
    }
    }

    //对文件进行批下载打包

    public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
    {

    #region INameTransform 成员

    public string TransformDirectory(string name)
    {
    return null;
    }

    public string TransformFile(string name)
    {
    return Path.GetFileName(name);
    }

    #endregion


    /// <summary>
    /// 批量进行多个文件压缩到一个文件、
    /// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
    /// <param name="zipFileName">生成的zip文件名称</param>
    /// </summary>
    public void ZipFileDownload(string[] files, string zipFileName,HttpContext Context)
    {
    MemoryStream ms = new MemoryStream();
    byte[] buffer = null;

    using (ZipFile file = ZipFile.Create(ms))
    {
    file.BeginUpdate();

    file.NameTransform = new MyNameTransfom();
    foreach (var item in files)
    {
    if (File.Exists(Context.Server.MapPath(item)))
    file.Add(Context.Server.MapPath(item));

    }
    file.CommitUpdate();
    buffer = new byte[ms.Length];
    ms.Position = 0;
    ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
    ms.Flush();
    ms.Close();
    }
    Context.Response.Clear();
    Context.Response.Buffer = true;
    Context.Response.ContentType = "application/x-zip-compressed";
    Context.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName));
    Context.Response.BinaryWrite(buffer);
    Context.Response.Flush();
    Context.Response.End();
    }
    }

    }

  • 相关阅读:
    H5学习的第三周
    2017.3.12 H5学习的第一周
    js中比较实用的函数用法
    JS学习中遇到的一些题目
    H5学习第四周
    idea快捷键
    中国国内可用API合集
    ssm整合 idea+maven版
    Dubbo的使用及原理浅析
    $.extend 和$.fn.extend的区别
  • 原文地址:https://www.cnblogs.com/liwp/p/6018667.html
Copyright © 2020-2023  润新知