Asp. NET implementation of the method of pushing files to the browser

Source: Internet
Author: User

The example of this article describes the ASP.net implementation of the push file to the browser method. Share to everyone for your reference. The specific analysis is as follows:

Here the main implementation from the server to the browser, push files, provide users to download/browse the function.

Hint: It will not work in Ajax UpdatePanel. If the code is invoked from a button-click event, the button needs to be outside of the AJAX UpdatePanel.

The specific code is as follows:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <summary>///Downloads (pushes) file to the client browser. The NOTE * * * * * * * cannot is done from inside a AJAX UpdatePanel control. </summary>///<param name= "Fullfilepath" >the full file path of the file</param> protected void LoadFile (String fullfilepath) {//Gets the File Name string fileName = fullfilepath.substring (Fullfilepath.lastindexof (' ') + 1); byte[] buffer; using (FileStream FileStream = new FileStream (Fullfilepath, FileMode.Open)) {int fileSize = (int) filestream.length; buffe r = new Byte[filesize]; Read file into buffer filestream.read (buffer, 0, (int) fileSize); } response.clear (); Response.Buffer = true; Response.bufferoutput = true; Response.ContentType = "Application/x-download"; Response.AddHeader ("Content-disposition", "attachment; Filename= "+ fileName); Response.CacheControl = "public"; Writes buffer to OutputStream Response.OutputStream.Write (buffer, 0, buffer. Length); Response.End (); }

          I hope this article will help you with your asp.net programming.

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.