Friday, February 25, 2011

Opening a Pdf file in a seperate windows using ASP.NET

How it works:

1. File uploader is used to get the pdf file input.

2. The pdf stream is recieved in the form of bytes using fupload.PostedFile.InputStream.Read and it is stored in the byte variable.

3. The stream variable is stored in the session.

4. The Read Document button opens the pdf file with the help of windows.open.

5. The child pop up usually spits up the session pdf stream with the help of Response.BinaryWrite.

Two files has been used. 
one to upload the pdf file (AcrobatReader.aspx) and the other to show the pdf file(Reader.aspx).

Functionality to received the pdf stream from fileupload control and open a child popup window

  if (fupDocument.HasFile && fupDocument.PostedFile.ContentLength > 0)
  {
      HttpPostedFile myFile = fupDocument.PostedFile;
                   
      Byte[] buffer = new byte[myFile.ContentLength];
      myFile.InputStream.Read(buffer, 0, myFile.ContentLength);
      Session["buffer"] = buffer;

      Response.Write("<SCRIPT language=javascript>var  pdf=window.open('Reader.aspx','pdf','width=600,height=400');pdf.moveTo(0,0);</SCRIPT>");
  }

Functionality to display the pdf stream

    Byte[] buffer = (byte[]) Session["buffer"];
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", buffer.Length.ToString());
    Response.BinaryWrite(buffer);

    Session["buffer"] = null;





No comments:

Post a Comment