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.