让我们一起爱米兰
站内搜搜:
移动设备
请扫描二维码
或访问
m.milan100.com
您所在的位置 -> 米兰百分百 -> ASP -> asp的Response.ContentType 控制输出文件类型

asp的Response.ContentType 控制输出文件类型

点击数:1157 发表时间:2010-07-27 11:28:13 作者: 来源链接:
分享到:
分享到微信

Response.ContentType :控制输出的文件类型 服务器送给客户端的数据包类型可以是text/html文本,也可以是gif/jpeg图形文件,所以每次传输前,我们都必须告知客户端将要传输的文件类型,一般默认情况下为“Text/Html”类型。
<% Response.ContentType = "text/HTML" %>
<% Response.ContentType = "image/GIF" %>
<% Response.ContentType = "image/JPEG" %>
<% Response.ContentType = "text/plain" %>
<% Response.ContentType = "image/JPEG" %>
<% Response.ContentType = "application/x-cdf" %>
用于作为文本内容返回而不是已解释的 HTML 语句
Response.ContentType = "text/plain"

 

<%
Response.ContentType = "text/plain"
Response.write(now()&"会被执行么?")
%>


你可以注意到:页面提供下载,页面中的ASP内容被解释执行了的

程序文件以XLS文件被提供下载
Response.ContentType = "application/vnd.ms-excel"

 

<%
Response.ContentType = "application/vnd.ms-excel"
Response.write("本页面调试会出现下载对话框提供下载,保存类型为XLS")
%>

 

实现歌曲连续播放
response.ContentType="audio/x-pn-realaudio"

 

<%
dim ramstr
ramstr=""
set rs=server.createobject("adodb.recordset")
sql="XXXXXXXXXXX"
rs.open sql,conn,1,3 ´conn已定义
do while not rs.eof
ramstr=ramstr&rs("url")&vbCrLf
rs.movenext
loop
rs.close
response.ContentType="audio/x-pn-realaudio"
´response.ContentType="audio/x-mpegurl"
response.write ramstr
%>

 

response.write 输出的时候,由于定义了response.ContentType 所以输出歌曲地址的时候会自动调用符合相应格式的软件来播放歌曲,不过前提是播放歌曲的软件必须先安装的。

这里我想讨论一下,这么也东东。

Q:如何利用ContentType 来,在服务器上提供一个.xls后缀的文件点击下载而不是直接在浏览器中打开。(注意:于上程序文件以XLS文件被提供下载有所不同)

Response.ContentType = "application/x-download",让整个程序文件点击下载了。怎么办好呢???

A:解决方案1. 利用Response.WriteFile的文件输出操作
具体在按钮点击事件中添加一下代码
private void btnDownload_Click(object sender, System.EventArgs e)
{
string DownloadFileName=Server.MapPath("file.xls");
string filepath = DownloadFileName;

// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);

Response.Clear();

// Specify the Type of the downloadable file.
Response.ContentType = "application/octet-stream";

// Set the Default file name in the FileDownload dialog box.
Response.AddHeader("Content-Disposition", "attachment; filename=" filename);

Response.Flush();

// Download the file.
Response.WriteFile(filepath);
}
以上代码也适合用于小于100MB的小文件下载
如果是大于100MB的大文件下载可以用Response.FileStream 。

C#代码如下:(将 DownloadFileName 替换为大于 100 MB 的文件的名称。)

System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new

0
很 好
0
一 般
0
差 劲
热门新闻
相关文章
上一篇: asp操作cookie
下一篇: ASP+MsSqlServer 存储过程分页
评论区
匿名

返回首页 | 收藏本页 | 回到顶部
Copyright 2010. 米兰百分百 Powered By Bridge.
京ICP备15050557号