ASP.NET使用文件流的方式下载文件

               //以字符流的形式下载文件

                string filePath = "D://dome.doc";
                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";

                string suffix = filePath.Trim().Substring(filePath.Trim().LastIndexOf("."));
                string fileName = Guid.NewGuid().ToString() + suffix;//保存的文件名
                //通知浏览器下载文件而不是打开
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();

猜你喜欢

转载自blog.csdn.net/sinat_21813453/article/details/84135413