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);
            }
        }

.net 文件下载方法的更多相关文章

  1. struts2 实现文件下载方法汇总

    http://pengranxiang.iteye.com/blog/259401 一.通过struts2提供的下载机制下载文件: 项目名为 struts2hello ,所使用的开发环境是MyEcli ...

  2. spring+jpg环境下,spring实现文件下载web实现通用的文件下载方法

    jar包复制到WEB-INF 文件夹lib下: commons-io-1.3.2.jar public static String download(HttpServletRequest reques ...

  3. .net文件下载方法汇总

    转载自:http://blog.sina.com.cn/s/blog_680942070101ahsq.html //TransmitFile实现下载 protected void Button1_C ...

  4. Xutils呼叫流源代码文件下载方法

    //我主要是好奇Xutils哪里回调onLoading(),查找等了很久也没找到,果然easy查找只是把它写下来 前言: 1.代码摘要只有主线,提供一般流程 2.为了易于理解,码变量名,而是类名的驼峰 ...

  5. .net core webapi带权限的文件下载方法

    众所周知,在webapi中,如果有个接口需要权限,一般会将带权限的字段塞进header中.但是,在带权限的文档下载接口中,无论是用post,还是get方式,我们无法设置header头信息.苦恼呀?别急 ...

  6. base64格式文件下载方法

    下载图片时,接口返回的地址是base64格式的文件数据,因为页面需要把base64格式的数据转换为文件,再进行下载: 解决方案: 下载按钮: <el-button type="defa ...

  7. 一个简单的PHP文件下载方法 download

    <?php /* * *@param function downloadFile 文件下载 * *@param string $filename 下载文件的路径(根目录下的绝对路径) * *@p ...

  8. php文件下载方法收藏(附js下载技巧)

    function down($url){ header('Content-Description: File Transfer'); header('Content-Type: application ...

  9. Chrome浏览器M53更新后超链接的dispatchEvent(evt)方法无法触发文件下载

    一个经典的js前台文件下载方法: var aLink = document.createElement('a'); var datatype="data:text/plain;charset ...

随机推荐

  1. jquery easy ui 学习 (7) TreeGrid Actions

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. JQuery学习笔记【CSS选择符】--02

    Jquery的程序入口: <html> <head> <title></title> <script type="text/javasc ...

  3. Linux——搭建PHP开发环境第一步:apache

    原文链接:http://www.2cto.com/os/201511/450258.html ##### Apache 编译安装[root@localhost ~]# yum install gcc ...

  4. 【Git】Git远程操作详解

    Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...

  5. 《how to design programs》13章用list构造表

    使用cons构造一个包含多个元素的表十分麻烦,因此scheme提供了list操作,该操作接受任意量的值作为输入以创建一个表,下面是扩展的语法: <prm>=list 扩展的scheme值的 ...

  6. 【HDOJ】1053 Entropy

    构造huffman编码,果断对字符进行状态压缩. #include <iostream> #include <cstdio> #include <cstring> ...

  7. Delphi 托盘程序实现 转

    unit MainUnit; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, F ...

  8. Windows打印管理解决方案

    需求 从需求出发,我们的目的是在电脑上提供一个虚拟打印机,然后让用户选择这个虚拟机打印时产生的中间文件被拦截下来,之后进行进一步处理后在执行真实的打印. Windows打印体系 首先附上查找Windo ...

  9. C++ 通过Thunk在WNDPROC中访问this指针

    本文基本只讨论原理,具体实现请参见后续文章<C++ 通过Thunk在WNDPROC中访问this指针实现细节> 当注册窗口类时,WNDCLASSEX结构的lpfnWndProc成员应设置为 ...

  10. Event — Windows API

    Event即事件是一种用于进行线程/进程间同步的对象,事件有置位和复位两种状态,当线程通过waiting functions等待Event对象置位时该线程将进入阻塞状态,当该Event对象被置位或等待 ...