本文給大家分享一段執行個體代碼關於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基礎視頻教程