Tutorials Forums
     Tutorials Videos
        Sign Up Now For FREE
Welcome, Guest
Username Password: Remember me

Pushing File to Browser
(1 viewing) (1) Guest
ASP.NET PROGRAMS, ASP.NET DESIGN, ASP.NET CODE, ASP.NET TOOLS

ASP.NET combines Active Server Pages with the .NET Framework. This technology allows you to create dynamic web applications. ASP.NET pages work in all browsers. Best of all, ASP.NET lets you build web pages using less code than you need with classic ASP. If you’re using ASP.NET to build some applications for your website, you owe it to yourself to check out the articles in this section.
  • Page:
  • 1

TOPIC: Pushing File to Browser

Pushing File to Browser 09 Dec 2009 22:26 #85

This snippet will push a file from the server to the browser to allow the user to download/view it.
Instructions: Must include the "System.IO" reference.

*** NOTE ***
This will not work inside of an AJAX UpdatePanel. If this code is called from a button click event, that button will need to be outside of any AJAX UpdatePanel.

 
/// <summary>
/// Downloads (pushes) file to the client browser.
/// **** NOTE **** Cannot be done from inside an AJAX UpdatePanel control.
/// </summary>
/// <param name="fullFilePath">The full file path of the file</param>
protected void DownloadFile(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;
 
buffer = 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();
}
 
  • CE
  • OFFLINE
  • Administrator
  • Posts: 197
  • Karma: 78
CodersEngine
  • Page:
  • 1
Time to create page: 0.26 seconds