码迷,mamicode.com
首页 > 其他好文 > 详细

文件下载的多种方法

时间:2014-06-14 23:47:24      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:code   http   ext   com   文件   string   

if (Request["name"] != null)
        {
            try
            {
                string FileName = MyCrypt.Decrypt(Request["name"]);
                //string FilePath = Server.MapPath(string.Format("UploadFile/{0}", FileName));

                string strPhyPath = Server.MapPath(string.Format("UploadFile/{0}", FileName));//fileurl是文件的相对地址
                if (File.Exists(strPhyPath))
                {
                    //取文件大小
                    FileStream MyFileStream;
                    uint FileSize1;
                    MyFileStream = new FileStream(strPhyPath, FileMode.Open, FileAccess.Read, FileShare.None);
                    int iConverTemp = Convert.ToInt32(MyFileStream.Length);
                    FileSize1 = (uint)(iConverTemp);
                    MyFileStream.Close();

                    //存在,下载
                    Page.Response.ContentType = "APPLICATION/OCTET-STREAM";
                    Page.Response.AddHeader("Content-length", FileSize1.ToString());//下载文件长度
                    Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                    Page.Response.WriteFile(strPhyPath);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    //MessageBox.Show("该附件不存在或者已被删除,请联系管理员处理!", this);
                    Response.Write("<script>alert(‘该附件不存在或者已被删除,请联系管理员处理!‘);if(window.parent!=window) window.parent.close(); else {window.opener=null;window.open(‘,‘,‘_self‘);window.close();}</script>");
                    return;
                }

                //第二种下载方法
                ////Response.ContentType = "application/x-zip-compressed"; itjeff modified.
                //Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)));
                //Response.TransmitFile(FilePath);


                //第三种下载方法
                //Response.ContentType = "application/ms-winword";
                //Response.ContentEncoding = System.Text.Encoding.Default;
                //Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
                //Response.WriteFile(FilePath);
                //Response.End();


                //第四种下载方法
                ////以字符流的形式下载文件
                //FileStream fs = new FileStream(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(FileName, System.Text.Encoding.UTF8));
                //Response.BinaryWrite(bytes);
                //Response.Flush();
                //Response.End();

            }
            catch (Exception er)
            {
                Response.Write(string.Format("<script>alert(‘下载附件时发生异常,异常信息为:{0}‘);</script>", er.Message));
            }
        }

文件下载的多种方法,布布扣,bubuko.com

文件下载的多种方法

标签:code   http   ext   com   文件   string   

原文地址:http://www.cnblogs.com/itjeff/p/3786160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!