Response.ContentType 控制输出文件类型(讨论下载文件问题)
2024-07-21 02:24:13
供稿:网友
服务器送给客户端的数据包类型可以是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