zoukankan      html  css  js  c++  java
  • .net 文件下载方法

     public void DownLoadMethod(string FilePath)
            {
                string hzm = Path.GetExtension(FilePath).ToLower();
                string FileName = Path.GetFileNameWithoutExtension(FilePath);
                string Name = FileName + hzm;
                if (hzm.ToLower() == ".pdf")
                {
                    Response.ContentType = "application/pdf";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                    Response.TransmitFile(Server.MapPath(FilePath));
                }
                else
                    if (hzm.ToLower() == ".jpeg" || hzm.ToLower() == ".jpg")
                    {
                        Response.ContentType = "image/jpeg";
                        Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                        Response.TransmitFile(Server.MapPath(FilePath));
                    }
                    else
                        if (hzm.ToLower() == ".gif")
                        {
                            Response.ContentType = "image/GIF";
                            Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                            Response.TransmitFile(Server.MapPath(FilePath));
                        }
                        else
                            if (hzm.ToLower() == ".doc" || hzm.ToLower() == "rtf" || hzm.ToLower() == ".docx")
                            {
                                Response.ContentType = "Application/msword";
                                Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                                Response.TransmitFile(Server.MapPath(FilePath));
                            }
                            else
                                if (hzm.ToLower() == ".xls" || hzm.ToLower() == ".xlsx")
                                {
                                    Response.ContentType = "Application/x-msexcel";
                                    Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                                    Response.TransmitFile(Server.MapPath(FilePath));
                                }
                                else
                                    if (hzm.ToLower() == ".txt")
                                    {
                                        Response.ContentType = "text/plain";
                                        Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                                        Response.TransmitFile(Server.MapPath(FilePath));
                                    }
                                    else
                                        if (hzm.ToLower() == ".htm" || hzm.ToLower() == ".html")
                                        {
                                            Response.ContentType = "text/HTML";
                                            Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                                            Response.TransmitFile(Server.MapPath(FilePath));
                                        }
                                        else
                                        {
                                            if (hzm.ToLower() == ".zip")
                                            {
                                                //以字符流的形式下载文件
                                                FileStream fs = new FileStream(Server.MapPath(FilePath), FileMode.Open);
                                                byte[] bytes = new byte[(int)fs.Length];
                                                fs.Read(bytes, 0, bytes.Length);
                                                fs.Close();
                                                Response.ContentType = "application/octet-stream";
                                                //通知浏览器下载文件而不是打开
                                                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Name, System.Text.Encoding.UTF8));
                                                Response.BinaryWrite(bytes);
                                                Response.Flush();
                                                Response.End();
                                            }
                                        }
            }

    //下载 压缩文件 *.zip

         private void DownloadFiles(string url)
            {
                if (url.Equals(""))
                {
                    return;
                }
                try
                {
                    if (!File.Exists(Server.MapPath(url)))
                    {
                        return;
                    }
                    FileInfo fi = new FileInfo(Server.MapPath(url));
                    Response.Clear();
                    Response.ContentType = "application/octet-stream";
                    string fileName = fi.FullName.Substring(fi.FullName.LastIndexOf("\") + 1);
                    if (fileName.Length > 32 && fileName.Split('-').Length >= 5)
                    {
                        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName.Substring(fi.FullName.LastIndexOf("\") + 1).Substring(fi.FullName.Substring(fi.FullName.LastIndexOf("\") + 1).IndexOf('_') + 1), System.Text.Encoding.UTF8));
                    }
                    else
                    {
                        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fi.FullName.Substring(fi.FullName.LastIndexOf("\") + 1), System.Text.Encoding.UTF8));
                    }
                    Response.AppendHeader("Content-Length", fi.Length.ToString());
                    Response.WriteFile(fi.FullName);
                    Response.Flush();
                    Response.End();
                }
                catch (FileNotFoundException ex)
                {
                    Response.Write(ex.Message);
                }
            }

  • 相关阅读:
    操作系统 第二章 进程管理
    操作系统 第一章 概述(补充)
    第六次博客作业——团队总结
    专题(十三)watch
    专题(十二)find 查找
    JVM 排查工具介绍(二)Memory Analyzer 堆内存分析工具
    Linux 学习笔记之(二)curl命令
    centos openjdk 11 安装软件包获取方式
    软件工程课程总结
    小黄衫!又一次?
  • 原文地址:https://www.cnblogs.com/wwy224y/p/3556973.html
Copyright © 2011-2022 走看看