惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 看风者

C#+ArcEngine问题小结(一) 2005年的最后一天,2006年的第一场雪 Web Parts, Impersonate and Security Policy BS开发中常用的javascript技术 INF文件是什么 INF探索 客户端回调 xmlHTTP技术 SPS下的session不能用 [转] 通过ASP.net程序创建域帐户故障 C# 程序员参考--COM Interop 第一部分:C# 客户端教程 SPS服务器上出现的奇怪问题? C#里有没有操作AD的类? VB.NET和C#的比较 在ASP.NET页面中实现数据棒图 暗算 之 看风 Session丢失网友回答集锦 问题征解3:SQL SERVER的性能问题 问题征解2:SPS与AD(活动目录)结合带来的系统安全性问题
Response.ContentType :控制输出的文件类型
看风者 · 2005-11-13 · via 博客园 - 看风者

服务器送给客户端的数据包类型可以是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 所以输出歌曲地址的时候会自动调用符合相应格式的软件来播放歌曲,不过前提是播放歌曲的软件必须先安装的。

以上文章转贴自http://www.w269.com/infow269/1145w2691.htm
这里我想讨论一下,这么也东东。

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 Byte[10000];;

// Length of the file:
int length;;

// Total bytes to read:
long dataToRead;;

// Identify the file to download including its path.
string filepath = "DownloadFileName";;

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

try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);;//用文件流来处理

// Total bytes to read:
dataToRead = iStream.Length;;

Response.ContentType = "application/octet-stream";;//问题就在这里,解决百M关口
Response.AddHeader("Content-Disposition", "attachment;; filename=" + filename);;

// Read the bytes.

  while (dataToRead 〉 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);;

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);;

// Flush the data to the HTML output.
Response.Flush();;

buffer= new Byte[10000];;
dataToRead = dataToRead - length;;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);;
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();;
}
}

参考:PRB:Response.WriteFile 无法下载大文件
http://support.microsoft.com/default.aspx?scid=kb;;zh-cn;;812406#2