asp.net 下載程序文件

// -- path 下載文件所在的決對路徑  
// fineName 下載的文件名稱和後最
 private void ReadFileWriterBinary(string path, string fileName)
        {

            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(path);
            HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
            Stream readStream = myWebResponse.GetResponseStream();
            BinaryReader SplitFileReader = new BinaryReader(readStream);
            byte[] TempBytes;
            HttpContext.Current.Response.ContentType = "application/save";
            // HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("gb2312");

            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlPathEncode(fileName) + "\"");
            TempBytes = SplitFileReader.ReadBytes(Convert.ToInt32(myWebResponse.ContentLength));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.BinaryWrite(TempBytes);
            SplitFileReader.Close();
            readStream.Close();
            string strPath = Server.MapPath(path);

         

        }
-- 第二種方法
   if (!File.Exists(strPath))
            {
                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlPathEncode(fileName) + "\"");
                Response.TransmitFile(strPath);
                Response.Flush();
                Response.End();
            }
            else
            {
                throw new Exception("文件不存在..");
            }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章