C#相容各大瀏覽器的檔案下載執行個體詳解

來源:互聯網
上載者:User
本文給大家分享一段執行個體代碼關於css實現檔案下載功能,需要的的朋友參考下吧

1、css代碼

public void DownFile(string filePath ,string fileName ){ // filePath 檔案路徑 例如:/File/記錄.xlsx  // fileName 檔案名稱 例如:記錄.xlsx (要尾碼哦)Encoding encoding; // 申明編碼string outputFileName; // 輸出名字Debug.Assert(HttpContext.ApplicationInstance.Request.UserAgent != null, "HttpContext.ApplicationInstance.Request.UserAgent != null");string browser = HttpContext.ApplicationInstance.Request.UserAgent.ToUpper();// 微軟的瀏覽器和ie過濾if (browser.Contains("MS") && browser.Contains("IE")){outputFileName = HttpUtility.UrlEncode(filePath);encoding = Encoding.Default;}//Firefoxelse if (browser.Contains("FIREFOX")){outputFileName = fileName;encoding = Encoding.GetEncoding("GB2312");}else{outputFileName = HttpUtility.UrlEncode(fileName);encoding = Encoding.Default;}string absoluFilePath = Server.MapPath(filePath); //擷取上傳檔案路徑FileStream fs = new FileStream(absoluFilePath, FileMode.Open);byte[] bytes = new byte[(int)fs.Length];fs.Read(bytes, 0, bytes.Length);fs.Close(); //關閉流,釋放資源HttpContext.ApplicationInstance.Response.Clear();HttpContext.ApplicationInstance.Response.Buffer = true;HttpContext.ApplicationInstance.Response.ContentEncoding = encoding;HttpContext.ApplicationInstance.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", string.IsNullOrEmpty(outputFileName) ? DateTime.Now.ToString("yyyyMMddHHmmssfff") : outputFileName));Response.BinaryWrite(bytes);Response.Flush();HttpContext.ApplicationInstance.Response.End();}

2、html代碼

前端HTML 寫一個a標籤就好:如 <a href='DownFile' target='_blank'>檔案下載</a>

【相關推薦】

1. 精選:“php程式員工具箱”V0.1版本下載

2. ASP免費視頻教程

3. 李炎恢ASP基礎視頻教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.