使用Response.ContentType 来控制下载文件的类型

使用Response.ContentType 来控制下载文件的类型服务器送给客户端的数据包类型可以是text/html文本,也可以是gif/jpeg图形文件,所以每次传输前,我们都必须告知客户端将要传输的文件类型,一般默认情况下为“Text/Html”类型。1<%Response.ContentType=”text/HTML”%>2<%Response.ContentType=”image/GIF”%>3…

大家好,又见面了,我是你们的朋友全栈君。

服务器送给客户端的数据包类型可以是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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161612.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Nmap命令扫描详解

    Nmap命令扫描详解在网络技术中,端口(Port)包括逻辑端口和物理端口两种类型。物理端口指的是物理存在的端口,如ADSLModem、集线器、交换机、路由器上用于连接其他网络设备的接口,如RJ-45端口、SC端口等等。逻辑端口是指逻辑意义上用于区分服务的端口,如TCP/IP协议中的服务端口,端口号的范围从0到65535,比如用于浏览网页服务的80端口,用于FTP服务的21端口等。由于物理端口和逻辑端口数量较多,为

    2022年5月27日
    110
  • c语言程序设计打卡系统,C语言程序设计报告书学生考勤系统设计.doc[通俗易懂]

    c语言程序设计打卡系统,C语言程序设计报告书学生考勤系统设计.doc[通俗易懂]设计题目:学生考勤系统设计设计方案:该系统能考察学生的出勤情况,所以包括每个班学生的全部信息。每个学生是一条记录,包括姓名﹑性别﹑学号,对应于某天某门课程的出勤情况等。该系统可模拟考勤过程,记录考勤结果,并能在课程结束后按照设定的考勤评分标准自动给出每个学生的考勤分数。其中,学生可在本系统在线请假以及查看学期内的上课出勤信息。在线请假中,学生可以随时查看到请假的详细进展情况。同时,学生可以查看本期…

    2025年6月12日
    0
  • javascript Date format(js日期格式化)

    javascript Date format(js日期格式化)这个很不错,好像是csdn的Meizz写的://对Date的扩展,将Date转化为指定格式的String//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q)可以用1-2个占位符,//年(y)可以用1-4个占位符,毫秒(S)只能用1个占位符(是1-3位的数字)//例子://(newDate()).Format(“yyyy-MM-ddhh:mm:ss.S”)==>2006-07-0208:09:04.423//(newDate())

    2022年4月30日
    36
  • Scrapy爬虫案例-淘宝比价定向爬虫学习笔记

    Scrapy爬虫案例-淘宝比价定向爬虫学习笔记说明Scrapy爬虫案例-淘宝比价定向爬虫学习笔记学习教程:Python网络爬虫与信息提取授课老师:嵩天官方网站:https://python123.io教程链接:https://python123.io/index/courses/804“淘宝比价定向爬虫”实例功能描述目标:获取淘宝搜索页面的信息,提取其中的商品名称和价格理解:淘宝的搜索接口翻页的处理技术路…

    2022年6月26日
    24
  • 数据库课程设计-职工工资管理系统

    数据库课程设计-职工工资管理系统XXX大学《数据库原理及应用课程设计》设计报告…

    2022年5月18日
    39
  • vs2012卸载工具_teighax能卸载吗

    vs2012卸载工具_teighax能卸载吗vs2005的安装和部署功能打包時加入卸载功能:  方法一:  1.在打包項目中添加文件msiexec.exe(一般可在c:/windows/system32/下找到)  2.在文件系統視圖中選擇應用程序文件夾,在msiexec.exe上按右鍵,選擇創建快捷方式,重命名快捷方式為”卸载”.  3.更改此快捷方式的Arguments 为”/x {產品id}”,

    2022年9月23日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号