码迷,mamicode.com
首页 > Web开发 > 详细

MVC 下载文件

时间:2018-01-25 16:45:13      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:.text   span   connected   txt   exists   write   读取   bsp   tco   

        //(1)返回filestream   其中:“text/plain”是文件MIME类型
        public FileStreamResult download()
        {
            string fileName = "aaa.txt";//客户端保存的文件名
            string filePath = Server.MapPath("~/Document/123.txt");//路径
            return File(new System.IO.FileStream(filePath, System.IO.FileMode.Open), "text/plain", fileName);
        }
        //(2)返回file
        public FileResult downloadA()
        {
            string filePath = Server.MapPath("~/Document/123.txt");//路径
            return File(filePath, "text/plain", "welcome.txt"); //welcome.txt是客户端保存的名字
        }
        public ActionResult FileStreamDownload1()
        {
            var stream = new System.Net.WebClient().OpenRead("http://files.cnblogs.com/ldp615/Mvc_TextBoxFor.rar");
            return File(stream, "application/x-zip-compressed", "test.rar");
        }

 

//(3)TransmitFile方法
        public void downloadB()
        {
            string fileName = "aaa.txt";//客户端保存的文件名
            string filePath = Server.MapPath("~/Document/123.txt");//路径
            System.IO.FileInfo fileinfo = new System.IO.FileInfo(filePath);
            Response.Clear();         //清除缓冲区流中的所有内容输出
            Response.ClearContent();  //清除缓冲区流中的所有内容输出
            Response.ClearHeaders();  //清除缓冲区流中的所有头
            Response.Buffer = true;   //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.AddHeader("Content-Length", fileinfo.Length.ToString());
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.ContentType = "application/unknow";  //获取或设置输出流的 HTTP MIME 类型
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //获取或设置输出流的 HTTP 字符集
            Response.TransmitFile(filePath);
            Response.End();
        }
        //(4)Response分块下载
        public void downloadC()
        {
            string fileName = "aaa.txt";//客户端保存的文件名
            string filePath = Server.MapPath("~/Document/123.txt");//路径

            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
            if (fileInfo.Exists == true)
            {
                const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
                byte[] buffer = new byte[ChunkSize];

                Response.Clear();
                System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
                long dataLengthToRead = iStream.Length;//获取下载的文件总大小
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
                while (dataLengthToRead > 0 && Response.IsClientConnected)
                {
                    int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                    Response.OutputStream.Write(buffer, 0, lengthRead);
                    Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }
                Response.Close();
            }
        }

 

MVC 下载文件

标签:.text   span   connected   txt   exists   write   读取   bsp   tco   

原文地址:https://www.cnblogs.com/enych/p/8351712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!