在a标签href属性直接写文件地址有些文件不会进入下载(例如 图片类型),浏览器会自动打开预览
这时可以使用下面这种方式进行文件下载
Html代码
<a href="/DownloadFile?fileName=fileName&path=path">点击下载</a>
C#代码
/// <summary> /// 文件下载 /// </summary> /// <param name="fileName">文件名(下载后的名字)</param> /// <param name="path">文件路径</param> 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(); }