• C# 下载文件


    1.0下载文件

       public void DownLoadeFile(string filePath, string name)
            {
                try
                {
                    filePath = Server.MapPath(filePath);
                    if (System.IO.File.Exists(filePath))
                    {
                        FileInfo info = new FileInfo(filePath);
                        long fileSize = info.Length;
                        Response.Clear();
                        Response.ContentType = "application/octet-stream";
                        Response.AddHeader("Content-Disposition", "attachement;filename=" + name);
                        //指定文件大小   
                        Response.AddHeader("Content-Length", fileSize.ToString());
                        Response.WriteFile(filePath, 0, fileSize);
                        Response.Flush();
                    }
                }
                catch
                { }
                finally
                {
                    Response.Close();
                }
            }

    2.0 打压缩包下载

    /// <summary>
            /// 文件打包下载
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnDownLoad_Click(object sender, EventArgs e)
            {
    
                DataTable dt = SelectA132(domainOrderId);
                List<string> filesPathList = new List<string>();
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["EnterpriseState"].ToString() == "1")//企业状态1代表单位0代表个人
                    {
                        filesPathList.Add(dt.Rows[0]["EnterpriseImgURL"].ToString());
                    }
                    filesPathList.Add(dt.Rows[0]["PersonalImgURL"].ToString());
                    if (filesPathList != null && filesPathList.Count > 0)
                    {
                        zip zip = new zip();
                        zip.DwonloadZip(filesPathList, dt.Rows[0]["domain"].ToString() + ".zip");
    
                    }
                }
    
            }
      /// <summary>
        /// 压缩文件类
        /// </summary>
        public class zip
        {
            /// <summary>
            /// 所有文件缓存
            /// </summary>
            List<string> filesPathList = new List<string>();
            /// <summary>
            /// 所有空目录缓存
            /// </summary>
            List<string> emptyPathList = new List<string>();
            /// <summary>
            /// 多文件打包下载
            /// </summary>
            /// <param name="filesPathList"></param>
            /// <param name="zipName"></param>
            public void DwonloadZip(List<string> filesPathList, string zipName)
            {
                MemoryStream ms = new MemoryStream();//存储区内存流
                byte[] buffer = null;
                var context = HttpContext.Current;
                using (ZipFile zfile = ZipFile.Create(ms))
                {
                    zfile.BeginUpdate();
                    /*通过这个名称格式化器,可以将里面的文件名进行一些处理。
                    默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。*/
                    zfile.NameTransform = new MyNameTransform();
                    foreach (var it in filesPathList)
                    {
                        if (!string.IsNullOrEmpty(it))
                        {
                            zfile.Add(it);
                        }
                    }
                    zfile.CommitUpdate();
                    buffer = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(buffer, 0, buffer.Length);
                }
                context.Response.AddHeader("content-disposition", "attachment;filename=" + zipName);
                context.Response.BinaryWrite(buffer);
                context.Response.Flush();
                context.Response.End();
            }
    
    
    
        }
  • 相关阅读:
    Google API 详解
    Why should I use SASS?
    Google Maps and ASP.NET
    IP摄像机
    解决母版页报错“内容控件必须是内容页中的顶级控件,或是引用母版页的嵌套母版页。”
    sass服务
    C#中操作符的重载(Time类)
    第一次面试
    单链表(C++)
    指针和引用的区别(C++)
  • 原文地址:https://www.cnblogs.com/tianxiaziwei/p/5626865.html
Copyright © 2020-2023  润新知