response下载文件 (转载)
核心代码:
DataSet ds = dBll.GetList("ID=" + ID); |
string docName = "a.doc";//文件名, |
byte[] file = (byte[])ds.Tables[0].Rows[0]["FContent"]; // "FContent"类型为image string Type = checktype(docName); Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(docName, System.Text.Encoding.UTF8).Replace("+", "%20")); Response.AddHeader("Content-Length ", file.Length.ToString()); Response.ContentType = Type; Response.BinaryWrite(file);//可以下载二进制文件,如数据库存的image //Response.WriteFile(fliePath);可以下载路径文件 Response.End(); Response.Clear();/// <summary>/// 根据文件的扩展名来获取对应的“输出流的HTTP MIME“类型/// </summary>/// <param name="filename"></param>/// <returns></returns>private string checktype(string filename){ string ContentType; switch (filename.Substring(filename.LastIndexOf(".")).Trim().ToLower()) { case ".asf ": ContentType = "video/x-ms-asf "; break; case ".avi ": ContentType = "video/avi "; break; case ".doc ": ContentType = "application/msword "; break; case ".zip ": ContentType = "application/zip "; break; case ".xls ": ContentType = "application/vnd.ms-excel "; break; case ".gif ": ContentType = "image/gif "; break; case ".jpg ": ContentType = "image/jpeg "; break; case "jpeg ": ContentType = "image/jpeg "; break; case ".wav ": ContentType = "audio/wav "; break; case ".mp3 ": ContentType = "audio/mpeg3 "; break; case ".mpg ": ContentType = "video/mpeg "; break; case ".mepg ": ContentType = "video/mpeg "; break; case ".rtf ": ContentType = "application/rtf "; break; case ".html ": ContentType = "text/html "; break; case ".htm ": ContentType = "text/html "; break; case ".txt ": ContentType = "text/plain "; break; default: ContentType = "application/octet-stream "; break; } return ContentType;} |
HttpUtility.UrlEncode(docName, System.Text.Encoding.UTF8).Replace("+", "%20"));这个可以解决中文文件名中包含空格的问题,UrlEncode编码后会把文件名中的空格转换中+(+转换为%2b),但是浏览器是不能理 解加号为空格的,所以在浏览器下载得到的文件,空格就变成了加号;
解决办法:UrlEncode 之后, 将 “+” 替换成 “%20″,因为浏览器将%20转换为空格
response下载文件 (转载)的更多相关文章
- Struts2学习笔记--使用Response下载文件和Struts2的StreamResult文件下载
使用Response下载文件,servlet中的文件下载是通过流来实现的 我在webRoot文件夹下新建了一个文件夹from,里边放了一张图片,这里就以下载这张图片为例:download.jsp很 ...
- servlet 中通过response下载文件
public class ResponseDemo3 extends HttpServlet { private static final long serialVersionUID = -52329 ...
- ajax请求不能下载文件(转载)
最近在做文件下载,后台写了个控制层,直接走进去应该就可以下载文件,各种文件图片,excel等 但是起初老是下载失败,并且弹出下面的乱码: 前台请求代码: $('#fileexcel').unbind( ...
- ajax中没法用response下载文件啊
ajax 下载不太现实第一,http 不支持直接的二进制传输,二进制数据需要编码 例如base64 ,这点服务器端可以实现第二,客户端获得编码后的文件要转换,js应该也可以第三点,最为致命,js无法操 ...
- response 下载文件
String basePath = "D://test.json"; String filename = basePath.substring(basePath.lastIndex ...
- ASP.NET Response 下载文件
private void DownLoad(string fileName, string path) { FileInfo fi = new FileInfo(path); if (fi.Exist ...
- response 下载文件火狐浏览器文件名乱码问题
string path = Server.MapPath(Url.Content("~/") + "UploadFiles/Template/"); ...
- Ajax下载文件(页面无刷新)
说明:Ajax是无法实现文件传输的,本文只是模拟了Ajax不刷新页面就可以请求并返回数据的效果.实质上还是通过提交form表单来返回文件流的输出. 分步实现逻辑: ajax请求服务器,访问数据库,根据 ...
- Servlet 上传下载文件
上传文件 1)在表单中使用表单元素 <input type=“file” />,浏览器在解析表单时,会自动生成一个输入框和一个按钮 2)表单需要上传文件时,需指定表单 enctype 的值 ...
随机推荐
- Dinic算法模板
详解:http://blog.csdn.net/wall_f/article/details/8207595 算法时间复杂度:O(E * V * V) #include <cstdio> ...
- xcode 创建项目 勾选 git 出现警告
1. 解决方法: 在mac机上找到终端工具 >> 这里先介绍下使用Command-line. 1,下载Git installer,地址:http://git-scm.com/downlo ...
- size_t总结
1.sizeof返回的必定是无符号整形,在标准c中通过 typedef 将返回值类型定义为size_t. 若用printf输出size_t类型时,C99中定义格式符%zd;若编译器不支持可以尝试%u或 ...
- 关于JQ的$.deferred函数。参考网络文档
由于jQuery版本问题对Deferred对象的实现有所不同,具体请参照jQuery api: jQuery.Deferred()基于Promises/A规范实现,因为jQuery本身的设计风格, ...
- apache 一域名下多个二级域名如何做设置?
域名最新配置说明官网:http://apache.chinahtml.com/ 目的是在根目录,不同子域名可以访问不同目录下的网站: 第一步:打开 C:\Windows\System32\driver ...
- Android first---文件读取(登录案例编写为主)
以android登录案例来介绍文件的读取与androidAPI给予的方法 第一步:绘制界面 绘制方法:在线性布局下面设置相对布局 代码部分: <LinearLayout xmlns:androi ...
- WebClient上传音频文件
//WebClient上传音频文件 public string UploadVoice(string fileNamePath) { Voice model=new Voice(); string s ...
- Flyweight
1 意图:运用共享技术有效地大量支持细粒度的对象 2 动机:flyweight是一个共享对象,可以在多个场景使用. 分为内部状态和外部状态,内部状态存储于flyweight中,包含了独立于flywei ...
- Laravel 流程分析——整体概论
从整体上来看(不考虑细节),Laravel流程相当简单,我们分析一下index.php文件(下面的第几行为实际代码,不是指文件的行) 第一行定义自动加载 require __DIR__.'/../bo ...
- 转Global.asax文件
Global.asax 文件是什么 Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序 ...