读文件输出
来源:互联网 发布:网络交流环境 编辑:程序博客网 时间:2024/06/18 14:41
public class FileResult : ActionResult { public FileResult() { } /// <summary> /// 文件显示名称必须带后缀名 /// </summary> public string FileName; /// <summary> /// 文件绝对路径 /// </summary> public string FilePaht; public override void ExecuteResult(ControllerContext context) { if (FileName.IndexOf(".") == -1) { int t = FilePaht.LastIndexOf("."); if (t != -1) { FileName += FilePaht.Substring(t); } } System.IO.Stream iStream = null; //定义缓冲区大小为300k int bufferLenth = 300000; byte[] buffer = new Byte[300000]; //文件长度 int length; try { //打开文件 iStream = new System.IO.FileStream(FilePaht, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); long dataToRead = iStream.Length;//文件总大小 context.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8; context.HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); context.HttpContext.Response.AddHeader("content-type", "application/octet-stream;charset=utf8"); //读取字节 while (dataToRead > 0) { context.HttpContext.Response.Clear(); bool cc = context.HttpContext.Response.SuppressContent; //判断客户端是否在线 if (context.HttpContext.Response.IsClientConnected) { length = iStream.Read(buffer, 0, bufferLenth); if (length < bufferLenth) { buffer = buffer.AsEnumerable().Take(length).ToArray(); } context.HttpContext.Response.BinaryWrite(buffer); try { if (length < bufferLenth) { context.HttpContext.Response.Flush(); buffer = new Byte[length]; dataToRead = dataToRead - length; } else { context.HttpContext.Response.Flush(); buffer = new Byte[bufferLenth]; dataToRead = dataToRead - length; } } catch { dataToRead = -1; } } else { dataToRead = -1; } } } catch (Exception ex) { context.HttpContext.Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { iStream.Close(); } } } }}
0 0
- 文件读人,输出
- 读文件输出
- 输出文件
- 输出文件
- 读文件输出数字码串
- 屏幕输出VS文件输出
- 输出文件的下载
- byte[] 文件输出
- 取得文件输出流
- 任意编码文件输出
- 输出流文件
- jsp输出excel文件
- PHP输出xls文件
- php输出excel文件
- 文件输入/输出
- 文件输入/输出
- 输出流文件
- matlab输出mif文件
- Chapter 5 Garbage Collectors
- SQLServer2008把数据导出成可执行的sql脚本
- Android NDK 开发(四)JNI 中局部引用、全局引用和弱全局引用
- ORACLE数据库处理数据中的换行符
- 各个布局xml属性
- 读文件输出
- QT自定义控件,以绘画板为列
- 二叉树的基本操作
- WebStrom快捷键
- Linux Shell: 快速进入深层目录
- select2 支持多值下拉框
- Oracle 12c DBC 创建oracle 实例
- 微服务架构下的数据一致性保证(三):补偿模式
- 提取EXE图标