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. JS键盘的键码(event.keyCode)

    keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...

  2. Linux文本操作三大利器总结:sed、awk、grep

    grep:(去除一行中需要的信息,同类与cut) grep全称是Global Regular Expression Print #常规用法 # grep -n root /etc/passwd :ro ...

  3. C++学习笔记--Season 2

    一个简单的EGE程序: #include "graphics.h" //EGE库的头文件 int main(int argc, char** argv) { initgraph(, ...

  4. 初识Java反射机制

    1.ClassLoader的类加载机制:并非一次性加载,而是需要的时候加载(运行期间动态加载)(Class文件加载到内存的代码段),static语句块在加载后执行一次.dynamic语句块(就是一个语 ...

  5. IDE 常用快捷键记录

    一:Eclipse (1)删除当前行:Ctrl+D (2)最大化当前编辑窗口:Ctrl+m (3)关闭当前编辑器窗口:Ctrl+F4/Ctrl+w (4)导入依赖包:Ctrl+Shift+o 二:Ne ...

  6. web2.0的几个开源项目

    web2.0的几个开源项目   Diggclone-开源digg类程序官方:http://www.talkingpixels.org/diggclone/index.php演示:同digg.com类似 ...

  7. BZOJ1715: [Usaco2006 Dec]Wormholes 虫洞

    1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 475  Solved: 263[Sub ...

  8. DataGridView DataGridViewCheckBoxColumn编辑时实时触发事件

    正常响应CellValueChanged()事件时,当改变checkbox状态时,只有当焦点离开该单元格时才能触发CellValueChanged()事件, 如果要改变checkbox值时实时触发Ce ...

  9. 怎样在WIN7系统下安装IIS和配置ASP(详细)

    一:Windows7系统 (IIS是WIN7自带的,版本7.0),首先是安装IIS.打开控制面板,找到“程序与功能”,点进去,点击左侧“打开或关闭Windows功能”,找到“Internet 信息服务 ...

  10. sql server 主从数据库同步 利用发布 订阅是实现

    发布订阅功能自SQL 2005之后就有了 配置方法可参考:http://blog.csdn.net/wanmdb/article/details/7515277 遇到问题: 1. 如果代理无法启动 , ...