Content-Disposition的使用和注意事项(转载)
我们在开发web系统时有时会有以下需求:
- 希望某类或者某已知MIME 类型的文件(比如:*.gif;*.txt;*.htm)能够在访问时弹出“文件下载”对话框
- 希望以原始文件名(上传时的文件名,例如:山东省政府1024号文件.doc)提供下载,但服务器上保存的地址却是其他文件名(如:12519810948091234_asdf.doc)
- 希望某文件直接在浏览器上显示而不是弹出文件下载对话框
- ……………………
要解决上述需求就可以使用Content-disposition来解决。第一个需求的解决办法是
"content-disposition",
public void ToDownload(string filename)
{
FileStream fileStream new FileStream(serverfilpath, FileMode.Open);
= fileStream.Length;
HttpContext.Current.Response.ContentType "application/octet-stream";
HttpContext.Current.Response.AddHeader("attachment; filename=\"" + );
////inline --- 在线打开
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
= byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
} static string serverfilpath, = new FileStream(serverfilpath, FileMode.Open);
= fileStream.Length;
HttpContext.Current.Response.ContentType "application/octet-stream";
HttpContext.Current.Response.AddHeader("inline; filename=\"" + );
HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
= byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
HttpContext.Current.Response.BinaryWrite(fileBuffer);
fileStream.Close();
HttpContext.Current.Response.End();
} static string filename)
{
return HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
}
简单的对上述代码做一下解析,ToDownload方法为将一个服务器上的文件(serverfilpath为服务器上的物理地址),以某文件名 (filename)在浏览器上弹出“文件下载”对话框,而ToOpen是将服务器上的某文件以某文件名在浏览器中显示/打开的。注意其中我使用了 UTF_FileName方法,该方法很简单,主要为了解决包含非英文/数字名称的问题,比如说文件名为“衣明志.doc”,使用该方法客户端就不会出现 乱码了。
需要注意以下几个问题:
- Content-disposition是MIME协议的扩展,由于多方面的安全性考虑没有被标准化,所以可能某些浏览器不支持,比如说IE4.01
- 我们可以使用程序来使用它,也可以在web服务器(比如IIS)上使用它,只需要在http header上做相应的设置即可
Content-Disposition的使用和注意事项(转载)的更多相关文章
- RBStoryboardLink库的使用注意事项 -转载至--坤哥MartinLi博客
RBStoryboardLink库的使用注意事项 demo下载:https://github.com/MartinLi841538513/RBStoryboardLinkDemo 操作步骤: 1,引入 ...
- 64位平台C/C++开发注意事项(转载)
转自http://coolshell.cn/articles/3512.html 在http://www.viva64.com/en/l/上例出了28个在64位平台上使用C/C++开发的注意事项,对于 ...
- CSS魔法堂:一起玩透伪元素和Content属性
前言 继上篇<CSS魔法堂:稍稍深入伪类选择器>记录完伪类后,我自然而然要向伪元素伸出"魔掌"的啦^_^.本文讲讲述伪元素以及功能强大的Contet属性,让我们可以通 ...
- Windows Server 2012 NIC Teaming介绍及注意事项
Windows Server 2012 NIC Teaming介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Windows Ser ...
- [Mime] MimeEntity--MimeEntity Mime实体帮助类 (转载)
点击下载 MimeEntity.rar 这个类是关于Mime实体的类看下面代码吧 /// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式 ...
- Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项
Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Window ...
- DIV+CSS+JS基础+正则表达式
...............HTML系列.................... DIV元素是用来为HTML文档内大块(block-level)的内容提供结构和背景的元素.DIV的起始 ...
- RFC3261--sip
本文转载自 http://www.ietf.org/rfc/rfc3261.txt 中文翻译可参考 http://wenku.baidu.com/view/3e59517b1711cc7931b716 ...
- post multipart data boundary问题 使用curl 向jersey post文件
原以为curl 模拟post file跟post string类似,-d参数一加 ,header一加就完了,这次遇到个问题,却怎么都搞不定. curl模拟post提交 与客户端定的协议是: Heade ...
随机推荐
- visifire 图表双坐标轴 silverlight
public void CreateChart(Grid oGrid, ObservableCollection<ListItem> lBaseOilBar) { ...
- ul li a active jquery.cookie.js
div class="righter nav-navicon" id="admin-nav"> <div class="mainer&qu ...
- Iterable 超级接口
这是一个老祖宗,一代一代往下拨 collection 的方法如下,是一个跟接口方法如下,见API collection : add():添加一个元素 addAll():添加一组元素 clear(); ...
- HDU 4507 有点复杂却不难的数位DP
首先来说,,这题我wrong了好几次,代码力太弱啊..很多细节没考虑.. 题意:给定两个数 L R,1 <= L <= R <= 10^18 :求L 到 R 间 与 7 无关的数的平 ...
- C# 获取当前路径
// 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory F:\广告编辑系统新\taxi_edit\taxi_form\bin\Debug\ ...
- Fun with dynamicobject dynamic and the settings table
What came before In my previous post I discussed ways of making the settings table using Generics to ...
- hdu 5363Key Set
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5363 Problem Description soda has a set S with n inte ...
- Scala学习笔记--抽象成员
package com.evor.test1 class Test1 { } object Test1{ def main(args:Array[String]):Unit = { //类参数和抽象字 ...
- Codeforces 358D Dima and Hares
http://codeforces.com/contest/358/problem/D 题意:给出n个数,每个数取走的贡献与相邻的数有关,如果取这个数的时候,左右的数都还没被取,那么权值为a,如果左右 ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(1)--后端 chsakell分享了前端使用AngularJS,后端使用ASP.NET Web API的购物车 ...