利用Response的WriteFile方法输出一些文件

        string path = Server.MapPath("~/字符串专题.doc");//文件的路径

        System.IO.FileInfo file = new System.IO.FileInfo(path);

        Response.Clear();

        Response.Charset = "utf-8";//设置输出的编码

        Response.ContentEncoding = System.Text.Encoding.UTF8;

        // 添加头信息,为"文件下载/另存为"对话框指定默认文件名   

        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));

        // 添加头信息,指定文件大小,让浏览器能够显示下载进度   

        Response.AddHeader("Content-Length", file.Length.ToString());

        // 指定返回的是一个不能被客户端读取的流,必须被下载   

        Response.ContentType = "application/msword";

        // 把文件流发送到客户端   

        Response.WriteFile(file.FullName);

        Response.End();

重要说明:当您在 ASP.NET 应用程序的 Web.config 文件中将编译元素的 debug 属性值设置为 false 时,必须针对要下载的文件大小将 server.scripttimeout 属性设置为适当的值。默认情况下,server.scripttimeout 值被设置为 90 秒。但是,当 debug 属性被设置为 true 时,server.scripttimeout 值将被设置为一个非常大的值(30,000,000 秒)。

posted @ 2012-10-18 17:01  zorro8z8  阅读(236)  评论(0编辑  收藏  举报