"Go" C # implementation file download

Source: Internet
Author: User

Requirements: Click Save, go to Ajax to download files in the background, after the completion of the Save dialog box, and then download directly.

Solution: After the download is complete, use JS to go to another page, in this page Page_Load with C#response object directly download.

Download the implementation code

 Private voidDownfile () {Try      {         Longlngfilesize; byte[] bytbuffer; intireading; stringsFileName = Server.MapPath ("TextFile.txt"); Stream OutStream= Response.outputstream;//Get output stream//Set Htttp headerResponse.ContentType ="Application/zip"; Response.appendheader ("Connection","Close"); Response.appendheader ("content-disposition","attachment; filename = ItemMarc.txt");//default file name when downloadFileStream FStream =NewFileStream (sFileName, FileMode.OpenOrCreate, FileAccess.Read); Lngfilesize=fstream.length; Bytbuffer=New byte[(int) lngfilesize];  while(ireading = Fstream.read (Bytbuffer,0, (int) lngfilesize) >0) {outstream.write (Bytbuffer,0, ireading);         } fstream.close ();         Outstream.close ();      Response.End (); }      Catch(Exception ex) {Throwex; }         }

Explanation: 1.response.contenttype = "Application/zip";

Content-type is very important in the return message, it identifies the type of the returned content, the value of the "main type/subtype" format, for example, the most common is text/html, which means that the returned content is the text type, the text is HTML format. In principle, the browser will decide how to display the returned message body content according to Content-type. Common types of content are:
text/html HTML text
image/jpeg jpg Images
Image/gif gif pictures
Application/xml XML Document
Audio/x-mpegurl MP3 file list, if Winamp is installed, you can open it directly to your face m3u file.

2.response.appendheader ("Connection", "close");

It is possible to have a connection header in the request and Reponse header in http1.1, which means how long links are handled when client and server communicate.
In http1.1, both the client and server are the default side support long links, if the client uses the HTTP1.1 protocol, but do not want to use long links, you need to indicate in the header the value of connection is close; if the server side Nor do you want to support long links, it is also necessary to explicitly state that the value of connection is close in response.
A connection with a value of close is included in the header of either request or response, indicating that the TCP link currently in use is broken when the request is processed on the same day. A new TCP link must be created in the future when the client makes a new request.

3. Response.appendheader ("Content-disposition", "attachment"; filename = ItemMarc.txt ");

Content-disposition: If you add the content-disposition segment to the HTTP message header with the AddHeader method and specify its value as "attachment", regardless of the type of the file, The browser will prompt us to download the file, because at this point it considers the message body to be an "attachment" and does not need to be handled by the browser. With the AddHeader function, we can add our own custom content to the HTTP message header. Using this method, you can force the browser prompt to download the file, even if the file is of the type we know, based on an HTML page. If you want the user to be prompted for a default file name when downloading, simply add "filename= file name" to the preceding sentence.

What if the combination of Content-type and content-disposition is used?
When a Web page is opened, the browser will first see if there is a content-disposition:attachment, and if so, the file will be downloaded regardless of the value of Content-type.
If filename is specified, the default file name is prompted for this file name.

4.response.end (); To output the cache to the client, the actual behavior, if not, will return all the default HTML content to the server.

"Go" C # implementation file download

Related Article

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.