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. Python展开一个嵌套的序列

    摘自<Python Cookbook> 4.6 任务 序列中的子序列可能是序列,子序列的子项仍有可能是序列,以此类推,则序列嵌套可以达到任意的深度.需要循环遍历一个序列,将其所有的子序列展 ...

  2. EMS-keil C51常用错误

    1. LAB100.C(12): error C216: subscript on non-array or too many dimensions 原程序如下: #include <reg51 ...

  3. 7.4.1 Dumping Data in SQL Format with mysqldump

    7.4 Using mysqldump for Backups 使用mysqldump 用于备份: 7.4.1 Dumping Data in SQL Format with mysqldump 7. ...

  4. !!!全球最流行开源硬件平台!不知道就OUT了!

    全球最流行的几个开源硬件平台!不知道就OUT了! 随着物联网的推广和普及,五年内全球会有200亿台智能设备的需求,而如今随着创客概念的兴起,开源硬件也越加的火热,让我们来看看现在都有哪些主流的开源硬件 ...

  5. Xen、KVM和VirtualBox比拼

    vbox 与 kvm 的区别: vbox 是由 qemu 改写而成,包含大量 qemu 代码.可以使用于 不支持 虚拟化的cpu.值得说的一点:vbox 在图形方面比较好,能进行2D 3D加速.cpu ...

  6. 【转】如何开启notepad++函数列表功能

    原文网址:http://jingyan.baidu.com/article/4b07be3c41e05e48b380f3f6.html Notepad++是window下特有的一款开源编辑器软件,相信 ...

  7. HDOJ 1056 HangOver(水题)

    Problem Description How far can you make a stack of cards overhang a table? If you have one card, yo ...

  8. Java 设计模式实现 不错的引用

    这段时间有兴趣重新温习一下设计模式在Java中的实现,碰巧看到一个不错的设计模式总结,这里引用一下作为参考. 创建型模式: JAVA设计模式-Singleton JAVA设计模式-Factory JA ...

  9. remastersys

  10. B - Dining - poj 3281(最大流)

    题目大意:有一群牛,还有一些牛喜欢的食物和喜欢的饮料,不过这些牛都很特别,他们不会与别的牛吃同一种食物或者饮料,现在约翰拿了一些食物和饮料,同时他也知道这些牛喜欢的食物和饮料的种类,求出来最多能让多少 ...