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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。