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 的值 ...
随机推荐
- mybatis动态SQL标签的用法
动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...
- 普华永道高级JAVA面试记录
最近在考虑换个工作 原因?咱能不逗吗? 一面感觉发挥不错 二面之后累觉不爱 基本上浪费了半天的工资(好多钱啊~~~) PWD上海地址在浦东软件园 工作环境说实话没我现在工作的环境好,不过里面的人 ...
- php 全角半角转换
<?phpheader("Content-type: text/html; charset=utf-8");// 第一个参数:传入要转换的字符串// 第二个参数:取0,半角转 ...
- lievent源码分析:evbuffer
struct evbuffer定义在evbuffer-internal.h文件中. evbuffer结构内部保存一个以evbuffer-chain结构为节点的链表,evbuffer内部有两个分别指向首 ...
- SQL Server 2008教程和Microsoft® SQL Server® 2008 R2 SP2 - Express Edition下载
教程 SQL Server 2008 Tutorialhttp://www.quackit.com/sql_server/sql_server_2008/tutorial/ 数据库下载 Microso ...
- Flowplayer-encoding
SOURCE URL: https://flowplayer.org/docs/encoding.html Video encoding To ease the task of encoding yo ...
- Codeforces Round #356 (Div. 2)
A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 使用hue+oozi构建任务调度系统
oozie调度系统调研 1. 关于oozie的简单说明 oozie应用程序目录结构说明: 一个完整的oozie应用程序一般情况下至少包含三个文件及目录:job.properties.workflow. ...
- implicit operator
class Digit { public Digit(double d) { val = d; } public double val; // ...other members // User-def ...
- CSS 3 盒子属性
#box1{ width: 100px;height: 40px; border: 1px solid black;(1)内容沾满盒子的处理方式 所有的都要添加前缀,以便更好的浏览器兼容 1,ove ...