在a标签href属性直接写文件地址有些文件不会进入下载(例如 图片类型),浏览器会自动打开预览
这时可以使用下面这种方式进行文件下载Html代码
C#代码
////// 文件下载 /// /// 文件名(下载后的名字) /// 文件路径 public void DownloadFile(string fileName, string path) { HttpContext.Response.ContentType = "application/ms-download"; string s_path = path; WebClient wc = new WebClient(); wc.Encoding = Encoding.UTF8; byte[] temp_byte = wc.DownloadData(s_path); HttpContext.Response.Clear(); HttpContext.Response.AddHeader("Content-Type", "application/octet-stream"); HttpContext.Response.Charset = "utf-8"; HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); HttpContext.Response.AddHeader("Content-Length", temp_byte.Length.ToString()); HttpContext.Response.BinaryWrite(temp_byte); HttpContext.Response.Flush(); HttpContext.Response.Clear(); HttpContext.Response.End(); }