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

Asp.net MemoryStream下载未弹出下载对话框
我想以流的方式下载文件,使用的方法如下:

        private void WriteStreamToResponse(MemoryStream ms)
        {
            if (ms.Length > 0)
            {
                string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("Content-Length", ms.Length.ToString());//文件长度 
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);//文件名称
                Response.ContentType = "application/octet-stream";//获取或设置HTTP类型  

                byte[] buffer = new byte[65536];
                ms.Position = 0;
                int num;
                do
                {
                    num = ms.Read(buffer, 0, buffer.Length);
                    Response.OutputStream.Write(buffer, 0, num);
                }
                while (num > 0);

                Response.Flush();
            }
        }

传入的ms是有数据的,但是浏览器不弹下载对话框,文件也没有被下载!请问这是什么原因?谢谢!