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

  • 相关阅读:
    IE安全级别没法定制
    将应用部署到Websphere的context root根/
    SqlServer插入慢的问题解决
    SqlServer中用@@IDENTITY取最新ID不准的问题
    分享我的戒烟经验
    Telnet发邮件过程
    .net垃圾回收和CLR 4.0对垃圾回收所做的改进之三
    Please pay more attention to the character set of your database
    翻旧贴: 什么是对象?
    C# Code in ASPX Page
  • 原文地址:https://www.cnblogs.com/wwy224y/p/3556973.html
Copyright © 2011-2022 走看看