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. HTML&CSS基础学习笔记1.33-元素选择器

    元素选择器 最常见的 CSS 选择器是元素选择器.换句话说,文档的元素就是最基本的选择器: p { text-indent:10px; color:blue; } 什么情况下使用元素选择器,一般我们这 ...

  2. Python爬虫预备知识

    1.http编程知识 http中client 和server的工作模式 client和server建立可靠的tcp链接(在HTTP1.1中这个链接是长时间的,超时断开策略) client通过socke ...

  3. Xcode can't verify the identity of the server

    当升级了苹果系统到 OS X El Captain 之后  ,打开Xcode 有时候会报错 如图 而且打开 svn  也会出类似错误  点击continue  了  下次 还会 出现 .这个很好解决 ...

  4. WebApp之Meta标签 (关闭自动识别数字为电话号码或邮箱之类)

    iPhone上的Safari(还有些webkit android手机浏览器)会自动对看起来像是电话号码的数字串(包括已经加入连字符或括号格式化过的)添加电话链接,点击之后会询问用户是否想要拨打该号码. ...

  5. x86 构架的 Arduino 开发板Intel Galileo

    RobotPeak是上海的一家硬件创业团队,团队致力于民用机器人平台系统.机器人操作系统(ROS)以及相关设备的设计研发,并尝试将日新月异的机器人技术融入人们的日常生活与娱乐当中.同时,RobotPe ...

  6. 【贪心】XMU 1061 Ckp的约会

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1061 题目大意: n个任务(n<=1000),给定名字和开始.结束时间,求最多能 ...

  7. [C#错误] 未找到类型或命名空间名称" " (是否缺少 using 指令或程序集引用?)

    现象:编译项目时提示未找到类型或命名空间名称" " (是否缺少 using 指令或程序集引用?)解决方法:如果是未找到类型,检查是否引用了类型所在的命名空间,使用using指令.如 ...

  8. white-space norma nowrap强制同一行内显示所有文本文字,让所有文字内容中一排显示不换行

    日常我们为了让文字内容在一行内显示完,哪怕宽度不够也不能换行,我们可以使用white-space样式,但如果遇到了html br强制换行标签,无论是设置white-space与否都会被<br&g ...

  9. shell如何将文件上传至ftp

    #!/bin/bash ip=$ port=$ user=$ /usr/bin/lftp -p $port $ip <<EOF user $user $pwd set ftp:ssl-au ...

  10. Oracle分页查询与RowNum

    1. RowNum伪列 Oracle中,RowNum是一个伪列,表示当前记录是查询结果集中的第几条. RowNum在使用上应该注意,不能在where条件中用RowNum大于.大于等于.等于某个大于1的 ...