c# 實現點擊下載功能

轉自百度知道瀏覽器

 private void DownLoad(string strName, string strPath)
        {
            string fileName = strName;//客戶端保存的文件名
            string filePath = Server.MapPath(strPath);//路徑 
            //以字符流的形式下載文件
            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();
        }
相關文章
相關標籤/搜索