日期:2014-05-18  浏览次数:21029 次

怎样点击下载文件而不是直接打开
<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();