分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

MVC 下载文件

发布时间:2023-09-06 01:39责任编辑:顾先生关键词: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(); ???????????} ???????}

MVC 下载文件

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved