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 的值 ...
随机推荐
- Ugly Number
public class Solution { public bool IsUgly(int num) { ) return false; ) return true; *==num){ num = ...
- HttpContext.Current.Cache使用文件依赖问题
HttpContext.Current.Cache.Insert("FCacheMs", tb, New CacheDependency(HttpContext.Current.S ...
- 关于img 403 forbidden的一些思考
网页中经常需要显示图片给用户看,对网站本身来说有的图片是从本地图片服务器来的,但是一旦数量多了以后,磁盘空间又是一个问题. 所以有时就希望显示其他网站的Image,直接把其他网站的图片显示在我的网站上 ...
- dynamics_cast<>
#include <iostream> class A { public: A(){} ~A(){} ;} }; class B:public A { public: B(){} ~B() ...
- Eclipse默认标签TODO,XXX,FIXME和自定义标签[转]
http://www.blogjava.net/Guides/archive/2011/11/14/363686.html Eclipse中的一些特殊的注释技术包括: 1. // TO ...
- MySQL Workbench 6 不能删除数据等问题(“Error Code: 1175”) 和入门教程
网络资料收集 当用MySQL Workbench进行数据库的批量更新时,执行一个语句会碰到以下错误提示: Error Code: 1175 You are using safe...without a ...
- 查看SQL SERVER数据库运行参数和连接数
---查看当前数据库系统所有请求情况.我只列出了我认为比较重要有助于我解决问题的字段. SELECT ds.session_id, ds.status, Db_name(dr.database_id) ...
- JavaScript格式化时间
最近最练习遇到一个问题,就是从数据库中获取时间,利用EasyUI界面来显示时间类型的生日,发现它是按照毫秒来计算的long型数据,在界面显示并不能到达到一目了然,这里可以用两种方法来解决这个问题 在d ...
- C#中的多线程 - 同步基础
原文:http://www.albahari.com/threading/part2.aspx 文章来源:http://blog.gkarch.com/threading/part2.html 1同步 ...
- Rails,uva 514
题目:铁轨 题目链接:UVa514链接 题目描述: 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站的顺序编号为1-n.你的任务是判断是否能让它们按照某种特定的顺序进入B方向的铁轨并驶入车站.例 ...