日期:2014-05-17  浏览次数:20532 次

.NET文件下载
if (System.IO.File.Exists(Path))
        {
            string fileName = era.Ename;//客户端保存的文件名
            string filePath = Path;//路径

            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(long)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
        else
        {
            JS("alert('\"文件不存在\"');", this.Page);
        }
这是我的下载代码,现在代码能完整的运行,bytes 数组的大小跟文件大小也一样,问题执行完之后浏览器的下载框没有出现,即没有进行下载操作,谁帮忙看看。

------解决方案--------------------


 string Path = Server.MapPath("test.doc");
        if (System.IO.File.Exists(Path))
        {
            string fileName ="test.doc";//客户端保存的文件名