zoukankan      html  css  js  c++  java
  • MVC 下载文件

            //(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();
                }
            }
  • 相关阅读:
    Go并发编程实战 第2版 PDF (中文版带书签)
    DirectShow 应用开发过程
    Filter 原理
    DirectShow 常用函数总结
    COM 编程基础
    DirectShow 简介
    C++ 静态库与动态库以及在 Windows上 的创建、使用
    DirectShow 学习方法
    Qt 编译配置相关总结
    环境变量对于 VS 有什么用?
  • 原文地址:https://www.cnblogs.com/enych/p/8351712.html
Copyright © 2011-2022 走看看