服务器送给客户端的数据包类型可以是text/html文本,也可以是gif/jpeg图形文件,所以每次传输前,我们都必须告知客户端将要传输的文件类型,一般默认情况下为“Text/Html”类型。

1 <% Response.ContentType = "text/HTML" %>
2  <% Response.ContentType = "image/GIF" %>
3  <% Response.ContentType = "image/JPEG" %>
4  <% Response.ContentType = "text/plain" %>
5  <% Response.ContentType = "image/JPEG" %>
6  <% Response.ContentType = "application/x-cdf" %>

用于作为文本内容返回而不是已解释的 HTML 语句
Response.ContentType = "text/plain"

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

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

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

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

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

1
2  <%
3 dim ramstr
4 ramstr=""
5 set rs=server.createobject("adodb.recordset")
6 sql="XXXXXXXXXXX"
7 rs.open sql,conn,1,3 'conn已定义
8 do while not rs.eof
9 ramstr=ramstr&rs("url")&vbCrLf
10 rs.movenext
11 loop
12 rs.close
13 response.ContentType="audio/x-pn-realaudio"
14 'response.ContentType="audio/x-mpegurl"
15 response.write ramstr
16 %>

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

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

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

解决方案: 利用Response.WriteFile的文件输出操作
具体在按钮点击事件中添加一下代码

1 private void btnDownload_Click(object sender, System.EventArgs e)
2 {
3 string DownloadFileName=Server.MapPath("file.xls");
4 string filepath = DownloadFileName;
5
6 // Identify the file name.
7 string filename = System.IO.Path.GetFileName(filepath);
8
9 Response.Clear();
10
11 // Specify the Type of the downloadable file.
12 Response.ContentType = "application/octet-stream";
13
14 // Set the Default file name in the FileDownload dialog box.
15 Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
16
17 Response.Flush();
18
19 // Download the file.
20 Response.WriteFile(filepath);
21 }

以上代码也适合用于小于100MB的小文件下载
如果是大于100MB的大文件下载可以用Response.FileStream 。

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

1 System.IO.Stream iStream = null;
2
3 // Buffer to read 10K bytes in chunk:
4 byte[] buffer = new Byte[10000];
5
6 // Length of the file:
7 int length;
8
9 // Total bytes to read:
10 long dataToRead;
11
12 // Identify the file to download including its path.
13 string filepath = "DownloadFileName";
14
15 // Identify the file name.
16 string filename = System.IO.Path.GetFileName(filepath);
17
18 try
19 {
20 // Open the file.
21 iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
22 System.IO.FileAccess.Read,System.IO.FileShare.Read);//用文件流来处理
23
24
25 // Total bytes to read:
26 dataToRead = iStream.Length;
27
28 Response.ContentType = "application/octet-stream";//问题就在这里,解决百M关口
29 Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
30
31 // Read the bytes.
32 while (dataToRead > 0)
33 {
34 // Verify that the client is connected.
35 if (Response.IsClientConnected)
36 {
37 // Read the data in buffer.
38 length = iStream.Read(buffer, 0, 10000);
39
40 // Write the data to the current output stream.
41 Response.OutputStream.Write(buffer, 0, length);
42
43 // Flush the data to the HTML output.
44 Response.Flush();
45
46 buffer= new Byte[10000];
47 dataToRead = dataToRead - length;
48 }
49 else
50 {
51 //prevent infinite loop if user disconnects
52 dataToRead = -1;
53 }
54 }
55 }
56 catch (Exception ex)
57 {
58 // Trap the error, if any.
59 Response.Write("Error : " + ex.Message);
60 }
61 finally
62 {
63 if (iStream != null)
64 {
65 //Close the file.
66 iStream.Close();

转载于:https://www.cnblogs.com/meil/archive/2011/01/24/1113802.html

使用Response.ContentType 来控制下载文件的类型相关推荐

  1. response的响应头,下载文件

    response的响应头,下载文件 前端代码 <%@ page contentType="text/html;charset=UTF-8" language="ja ...

  2. java设置ContentType,设置下载文件名称

    java设置ContentType,设置下载文件名称 根据上传文件名设置ContentType 设置下载文件名称 根据上传文件名设置ContentType 几种常用上传文件如下: private St ...

  3. springboot传入json和文件_Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)...

    server: port: 8088 servlet: context-path: /sid spring: mvc: view: prefix: / suffix: .html /** * 返回界面 ...

  4. vue中下载文件导出保存到本地

    vue中下载文件导出保存到本地 先分析如何下载:先有一个链接地址,然后使用 location.href或window.open()下载到本地 看看返回数据 res.config.url 中是下载链接地 ...

  5. 实现在网页上下载文件

    title: 实现在网页上下载文件 date: 2015-12-28 20:34:19 categories: UtilsClass_Resource tags: Utils xl_echo编辑整理, ...

  6. Node.js实现下载文件

    第一种方式:使用原生的http模块 我们仅需要用到fs和http两个node.js的原生模块,不需要安装第三方模块,就可以实现文件的下载.代码如下: var fs = require('fs'); v ...

  7. HTML a链接下载文件之图片,文件,乱码等问题

    我们在做需求的时候,经常会遇到下载文件 前端下载文件一般分为两种方式: 使用 a 链接进行下载: <a herf="url" >下载</a> 向后端发送请求 ...

  8. python selenium 火狐下载文件

    注意: 1.需要加载 browser.helperApps.neverAsk.saveToDisk 选择,其值 application/x-msdownload 为Mine_type类型.该类型需要在 ...

  9. asp.net response.ContentType 下载文件的四种方法

    protected void Button1_Click(object sender, EventArgs e)     {  1 protected void Button1_Click(objec ...

最新文章

  1. Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件...
  2. 十进制与二进制间的相互转换
  3. windows下安装及配置 golang 的Web框架Beego环境
  4. 常见的爬虫分析库(1)-Python3中Urllib库基本使用
  5. 360推出国内首个工业互联网安全态势感知系统
  6. [转] 测试员,敢问路在何方(来自微软工程师)
  7. 基于STM32的DS1302时钟模块驱动程序
  8. 匈牙利算法 KM算法
  9. Oracle客户端使用
  10. ADT下载地址,完整版
  11. ProtoBuf(Google Protocol Buffers)—— repeated 修饰字段注意点(packed修饰)
  12. “一键GHOST”傻瓜式系统备份与恢复
  13. Mac操作系统下怎么显示隐藏文件
  14. 纯代码开发c# ui_UI代码挑战#1-心跳
  15. SQL Developer连接时报错:ORA-12528
  16. 小米手机中的快应用服务器,盘点MIUI里快应用2种开启方法
  17. C语言之如何求任意一个已知三边的三角形面积
  18. 使用xlwings插件在Excel中调用Python
  19. vue前端面试题之vue组件传递参数
  20. 分享几种Spring Boot常用数据处理方式(含代码,粘贴可用)

热门文章

  1. lua学习笔记之协程
  2. 批处理bat中的脚本
  3. 防止重复提交保证幂等的几种解决方案
  4. JDBC 此驱动程序不支持 Java Runtime Environment (JRE) 1.6 版
  5. source insight 4.0.086破解
  6. 【PAT】B1058 选择题(20 分)
  7. CentOS系统安装配置JDK
  8. Offer是否具有法律效力?
  9. Linux学习笔记(知识点总结)
  10. Dateset学习笔记