Learning to download files

Source: Internet
Author: User

ExampleDownload tranmitfile

Fileinfo UBF = new fileinfo (this. url. value );
Response. Clear (); // clear the response content before downloading the object, so as not to write redundant response, leading to an error in downloading the object
Response. clearheaders (); // clear the previous file to avoid conflict or error
Response. Buffer = false; // no buffer, while parsing and sending

Response. contenttype = "text/plain"; // specify the downloaded file format
Response. contentencoding = system. Text. encoding. Default; // you can specify the format of the downloaded file.
Response. appendheader ("Content-Length", UBF. length. tostring (); // specify the file length (optional), response. buffer = false; the total file size cannot be automatically obtained. You must specify the size as a file size prompt during download.
Response. appendheader ("content-disposition", "attachment; filename =" + "ddd.txt"); // content configuration, download with attachment (open online in inline), and download the file name
Response. transmitfile (this. url. Value); // This method does not write data to the buffer. the specified file is directly written to the HTTP Response output stream.

Response. Flush (); // sends the compiled data in the buffer to the client first.
Response. End ();

Example stream download

Response. Clear (); // clear the response content before downloading the object, so as not to write redundant response, leading to an error in downloading the object
Response. clearheaders (); // clear the previous file to avoid conflict or error
Response. Buffer = false;
Filestream downfile = new filestream (this. url. value. Trim (), filemode. Open, fileaccess. Read); // create a file stream and specify File Permissions
Byte [] DATA = new byte [downfile. Length]; // large file byte array overflow, resulting in an error
Downfile. Read (data, 0, Data. Length); // write the file as a byte array (Stream)
Downfile. Close ();
Response. contenttype = "application/octet-stream"; // you can specify the downloaded file format.
Response. contentencoding = system. Text. encoding. Default; // you can specify the format of the downloaded file.
Response. appendheader ("content-disposition", "attachment; filename =" + "ddd.txt"); // content configuration, download with attachment, and download the file name
Response. binarywrite (data );
Response. Flush ();
Response. End ();

Example

Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");
Response. charset = "UTF-8 ";
Response. Write ("Qianyi network ");
Then open the web page in a browser and you will find garbled characters. However, you can use NotePad to view the source file and find that it is not garbled. This shows that:

Contentencoding controls the flow of bytes to the text, while charset controls the display in the browser.

 

Example 1

Protected void btnresponseword_click (Object sender, eventargs E)
{
Response. Clear (); // clear irrelevant information
Response. Buffer = true; // send the entire response
Response. charset = "gb2312"; // set the character set of the output stream-Chinese
Response. appendheader ("content-disposition", "attachment?filename=report.doc"); // append header information
Response. contentencoding = system. Text. encoding. getencoding ("gb2312"); // you can specify the character set of the output stream.
Response. contenttype = "application/MS-word"; // MIME type of the output stream
Response. Write (textbox1.text );
Response. End (); // stop the output
}

Why use clear ():

Not required. If you are calling response. writefile (picturefilename); previously written data to the buffer, for example, called: response. write ("ABC"); and you want the browser to display the image normally, then you need response. clear (); if not response. clear (); then the image may not be displayed normally. Why? The reason is: first, you want to tell the client that the response content type is image/JPEG. You are using response. appendheader ("Content-Type", "image/JPEG"); declarative. Then, when the client obtains the response data, it will also interpret the data according to image/JPEG, but because you have previously used response. write ("ABC"); therefore, the actual content obtained by the client is <character: ABC> + <image data> in this way, the data obtained by the client is not only the image data, but the "Content-Type" you declare emphasizes that the data should be interpreted according to image/JPEG. the final result is that the image cannot be decoded normally. writefile (picturefilename); previously called response. clear (); relatively safe

Example 3

 

Fileinfo downloadfile = new fileinfo (URL );
Response. Clear ();
Response. clearheaders ();
Response. Buffer = false;
Response. charset = "UTF-8 ";
Response. contentencoding = system. Text. encoding. utf8;
Response. contenttype = "application/octet-stream ";

String agent = request. useragent;
String desvalue = string. empty;
If (agent! = NULL & agent. indexof ("MSIE", stringcomparison. currentcultureignorecase) =-1)
{
Desvalue + = "attachment; filename * = utf8 ''";
}
Else {
Desvalue = "attachment; filename = ";
}
Response. appendheader ("content-disposition ",
Desvalue + server. urlencode (bookservice. getentity (this. downpath. Value). Name + downloadfile. Extension ));
Response. appendheader ("Content-Length", downloadfile. length. tostring ());
Response. Flush ();
Response. transmitfile (downloadfile. fullname );
Response. Flush ();
Response. End ();

Response. Flush ().Many times, the ASP program we write will take one minute or even several minutes for many operations. To allow software users to patiently wait for program execution, we often want a progress bar to indicate the state of program execution. Or at least a descriptive text like "loading data" and "saving data" should be displayed. Now we will use response. Flush (). It sends the compiled data in the buffer to the client first.

 

That is, when the buffer is true, ASP will send the program to the client after it is explained,
When the buffer value is false, ASP parses and sends data.

<% Response. buffer = true %> indicates whether the output page is buffered. When the attribute value is true, the server will not send any information to the client, until all programs are executed or <% response. flush %> or <% response. to release the buffer information.

The following code demonstrates the buffer feature.
<%
Response. charset = "gb2312"
Response. Buffer = "false"
Dim str256
Str256 = "canonical"
Response. Write ("START <br> ")
For 1 to 100
For u = 1 to 1000000
Next
Response. Write (str256 &"")
Response. Write (I & "<br> ")
Next
Response. Write ("end ")
%>
<% Response. charset = "gb2312" response. Buffer = "false" dim str256 str256 = "Limit"
Response. write ("START <br>") for I = 1 to 100 for u = 1 to 1000000 next response. write (str256 & "") response. write (I & "<br>") next response. write ("ended") %>

If response. Buffer = "false" is changed to response. Buffer = "true", the parsing is completed and then output. That is, a webpage containing numbers 1 to 100 is output.

 

 

Response. transmitfile (filename );
Transmitfile msdn explains that the file is directly input into the HTTP output stream without caching
This means that the file is directly transmitted to the client, rather than the entire file is cached and transmitted to the client.
Therefore, if the file is too large, the request will be relaxed to keep the file downloaded.

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.