Response.ContentType control output File type (discussion download file issue)

Source: Internet
Author: User
response| Control | problem | The packet type that the download server sends to the client can be either text/html text or gif/jpeg graphics file, so each time we transmit, we must inform the client of the type of file that will be transferred, typically by default, "Text/html" Type.
<% 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"%>
Used to return text content instead of an interpreted HTML statement
Response.ContentType = "Text/plain"



<%
Response.ContentType = "Text/plain"
Will Response.Write (now &) be executed? ")
%>



You can note that the page provides downloads, and the ASP content in the page is interpreted to perform the

Program files are provided for download with XLS files
Response.ContentType = "Application/vnd.ms-excel"



<%
Response.ContentType = "Application/vnd.ms-excel"
Response.Write ("This page debugging will appear download dialog box provides download, save type is XLS")
%>




To achieve a continuous song playback
Response. Contenttype= "Audio/x-pn-realaudio"



<%
Dim ramstr
Ramstr= ""
Set Rs=server.createobject ("Adodb.recordset")
Sql= "Xxxxxxxxxxx"
Rs.Open sql,conn,1,3 ' conn defined
Do as 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 output, due to the definition of response. ContentType so output song address will automatically call the corresponding format of the software to play the song, but only if the software to play the song must be installed first.

The above article pastes from http://www.w269.com/infow269/1145w2691.htm
Here I want to discuss, so also dongdong.

Q: How to use ContentType to provide an. xls suffix file on the server by clicking on the download instead of opening it directly in the browser. (Note: On the program file with XLS file is provided to download a different)

Response.ContentType = "Application/x-download", let the whole program file click to download. What to do???

A: Solution 1. Using Response.WriteFile file-output operations
Add the code in the button click event specifically
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);
}
The above code is also suitable for small file downloads less than 100MB
If the large file is larger than 100MB download can be used Response.filestream.

The C # code is as follows: (replace DownloadFileName with the name of a file greater than 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);//Use file flow to process


Total bytes to read:
dataToRead = Istream.length;

Response.ContentType = "Application/octet-stream";//The problem is here, resolve the Hundred m pass
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ filename);

Read the bytes.
while (dataToRead > 0)
{
Verify That's 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 ();
}
}

Reference: PRB:Response.WriteFile cannot download large files
Http://support.microsoft.com/default.aspx?scid=kb;zh-cn;812406#2


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.