读书人

怎样点击下载文件而不是直接打开解决办

发布时间: 2012-05-31 12:19:24 作者: rapoo

怎样点击下载文件而不是直接打开
<a href="<%#Eval("path") %>" target="_blank">下载</a> 点击下载后是浏览器直接打开文件,怎么弄成下载文件

[解决办法]
默认情况下你的href,如果是浏览器识别的MIME类型,它会打开,如果不识别的就会显示下载,当然也和客户端设置有关系。
[解决办法]
试试:

C# code
        string filePath = Server.MapPath("~/yourCatalog/yourFile.xxx");        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("yourFile.xxx", System.Text.Encoding.UTF8));        Response.BinaryWrite(bytes);        Response.Flush();        Response.End(); 

读书人网 >C#

热点推荐