国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Response.ContentType 控制輸出文件類型(討論下載文件問題)

2024-07-21 02:24:13
字體:
供稿:網(wǎng)友
服務(wù)器送給客戶端的數(shù)據(jù)包類型可以是text/html文本,也可以是gif/jpeg圖形文件,所以每次傳輸前,我們都必須告知客戶端將要傳輸?shù)奈募愋停话隳J(rèn)情況下為“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" %>
用于作為文本內(nèi)容返回而不是已解釋的 html 語句
response.contenttype = "text/plain"



<%
response.contenttype = "text/plain"
response.write(now()&"會(huì)被執(zhí)行么?")
%>



你可以注意到:頁面提供下載,頁面中的asp內(nèi)容被解釋執(zhí)行了的

程序文件以xls文件被提供下載
response.contenttype = "application/vnd.ms-excel"



<%
response.contenttype = "application/vnd.ms-excel"
response.write("本頁面調(diào)試會(huì)出現(xiàn)下載對(duì)話框提供下載,保存類型為xls")
%>




實(shí)現(xiàn)歌曲連續(xù)播放
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 輸出的時(shí)候,由于定義了response.contenttype 所以輸出歌曲地址的時(shí)候會(huì)自動(dòng)調(diào)用符合相應(yīng)格式的軟件來播放歌曲,不過前提是播放歌曲的軟件必須先安裝的。

以上文章轉(zhuǎn)貼自http://www.w269.com/infow269/1145w2691.htm
這里我想討論一下,這么也東東。

q:如何利用contenttype 來,在服務(wù)器上提供一個(gè).xls后綴的文件點(diǎn)擊下載而不是直接在瀏覽器中打開。(注意:于上程序文件以xls文件被提供下載有所不同)

response.contenttype = "application/x-download",讓整個(gè)程序文件點(diǎn)擊下載了。怎么辦好呢???

a:解決方案1. 利用response.writefile的文件輸出操作
具體在按鈕點(diǎn)擊事件中添加一下代碼
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關(guān)口
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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浙江省| 武乡县| 沈阳市| 阳高县| 德江县| 梁河县| 潞城市| 临邑县| 临沧市| 和顺县| 建昌县| 金华市| 普陀区| 仙桃市| 雷州市| 阿克苏市| 乾安县| 伽师县| 泾源县| 贡嘎县| 信丰县| 瑞丽市| 正阳县| 阳曲县| 潮安县| 老河口市| 胶州市| 新民市| 涡阳县| 广南县| 岚皋县| 墨脱县| 马龙县| 称多县| 南投县| 南澳县| 高邮市| 邯郸县| 电白县| 神木县| 微博|