服务器通过浏览器下载文件到本地
来源:互联网 发布:简单游戏编程 编辑:程序博客网 时间:2024/05/01 05:19
/// <summary> /// 从服务器通过浏览器下载文件到本地 /// </summary> /// <param name="ExcelItemFile"></param> /// <param name="ExcelItemName"></param> public void GetFile(string ExcelItemFile, string ExcelItemName) { //提供创建、复制、删除、移动和打开文件的属性和实例方法,并且帮助创建 System.IO.FileStream 对象 //1.先创建一个文本信息对象 FileInfo fi = new FileInfo(ExcelItemFile);//ExcelItemFile为文件在服务器上的地址 //2.创建一个封装了HTTP的响应信息 HttpResponse contextResponse = HttpContext.Current.Response; //3.清空响应流中的内容 contextResponse.Clear(); //4.处理完整个响应后发送它 contextResponse.Buffer = true; //5.设置响应流的相关信息 contextResponse.Charset = "GB2312"; //设置了类型为中文防止乱码的出现 contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", ExcelItemName)); //定义输出文件和文件名 contextResponse.AppendHeader("Content-Length", fi.Length.ToString()); contextResponse.ContentEncoding = Encoding.Default; contextResponse.ContentType = "application/ms-ExcelItem";//设置输出文件类型为ExcelItem文件。 //6.将指定文件的内容作为文件块直接写入 HTTP 响应输出流。 contextResponse.WriteFile(fi.FullName); contextResponse.Flush(); contextResponse.End(); } /// <summary> /// 获取绝对地址 /// </summary> /// <param name="strPath"></param> /// <returns></returns> public static string GetMapPath(string strPath) { //判断是否是http请求 if (HttpContext.Current != null) { // 返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径 return HttpContext.Current.Server.MapPath(strPath); } else //非web程序引用 { strPath = strPath.Replace("/", "\\"); if (strPath.StartsWith("\\")) { strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\'); } //==AppDomain.CurrentDomain.BaseDirectory 获取基目录 //==System.IO.Path.Combine(s1,s2) 把s1和s2组合成一个新的目录 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); } } public void done() { GetFile( GetMapPath("ExcelItem.xlsx"), "ExcelItem.xlsx"); }
0 0
- 服务器通过浏览器下载文件到本地
- 浏览器客户端从服务器下载文件到本地
- 服务器下载文件到本地
- 下载(二)客户端通过C# 代码直接从服务器下载文件到本地
- 上传文件到服务器,从服务器下载文件到本地
- Java 从服务器下载文件到本地
- Java 从服务器下载文件到本地
- Java从服务器下载文件到本地
- 通过SecureCRT下载远程Linux服务器上的文件到本地Windows
- 通过SecureCRT下载远程Linux服务器上的文件到本地Windows
- 通过SecureCRT工具从远程Linux服务器下载文件到本地Windows
- 客户端通过C# 代码直接从服务器下载文件到本地
- xshell上传文件到服务器和下载文件到本地
- 从远程服务器下载文件到本地服务器
- 浏览器自动下载文件到本地,JavaScript中的ActiveXObject对象
- 关于Ajax无法下载文件到浏览器本地的问题
- 用浏览器下载(图片/文件)到本地
- scp从服务器下载文件到本地或从本地上传文件到服务器
- java观察者模式
- QT问题记录
- 例题1.8 彩色立方体 UVa1352
- Centos安装metasploit
- 杭电 1012 u Calculate e
- 服务器通过浏览器下载文件到本地
- Java 转换成ObjectC代码
- 99乘法表
- C++的Socket尝试
- 想要开发好的软件,必须学会这几项!
- 判断浏览器版本
- mysql 定时任务编写分类
- SSH之Spring
- Feekood基本语法(5)-- 动画