google analytics

Showing posts with label file streaming. Show all posts
Showing posts with label file streaming. Show all posts

Monday, May 4, 2009

File streaming in .NET

Hello All

Today i will focus on simple file steaming in .NET which I have to implement it in one of my project.Though it’s not optimize but its OK for serve small purpose.

In the project I have to read a PDF file from the server and with FileStream and Flash it with Response to the client side.

the code is as bellow.

private void generateRequestedPDF()
{
    try
    {
        string file = Path.GetFileName(Request.QueryString["RefID"]);// get file name from query string.use your custom method." 

        FileStream fileStream = new FileStream(Server.MapPath(@"~\uploadedFiles\" + file), FileMode.Open);
        long fileSize = fileStream.Length;

        byte[] buffer = new byte[(int)fileSize];
        fileStream.Read(buffer, 0, (int)fileSize);
        fileStream.Close();

        Response.Clear();

        Response.ContentType = "Application/pdf";// use your custom type"
        Response.AddHeader("Content-Disposition", "inline; filename=" + file);

        Response.OutputStream.Write(buffer, 0, (int)fileSize);
        Response.Flush();
        Response.Close();
    }
    catch (Exception exc)
    {
    //  throw exc;
        this.writeLogWarn(exc);
        Response.Redirect("~/Pages/Content/TechCorner.aspx");
    }
   
}
thats all for today.
BYE

User ScrumPad for your Agile based projects.