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

來源:互聯網
上載者:User
response|控制|問題|下載 伺服器送給用戶端的資料包類型可以是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


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。