Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2007, 03:59 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Open a PDF file in a web application

I was surfing the web, and I found a site that opens a PDF file in a new page, so I thought I'll share the method of how to that with everyone :)

In the main page add this line:
{
Response.Write("<script>window.open('frmReport.asp x?r=frmReport.aspx,'_new');</script>");
}

Where frmReport.aspx is the name of the form in which you will open the file and frmReport.aspx is the name of the file, which of course you can read dynamically.

Now in the frmReport.aspx ad the following code in the Page_Load.

protected void Page_Load(object sender, EventArgs e)
{
        string strFileName = Request.QueryString["r"];

        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/PDF";
        Response.WriteFile(strFileName);
        Response.Flush();
        Response.Close();
        Session.Remove("Report");

}

Notice that the name of the file is sent through the URL.

Hope this will help someone in need :p


Nothing is impossible. The impossible only takes longer. "Digital Fortress, Dan Brown"
__________________
Nothing is impossible. The impossible only takes longer. \"Digital Fortress, Dan Brown\"
 
Old April 10th, 2007, 05:47 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

as a little enhancement to the code i wrote before, replace
{
Response.ContentType = "application/PDF";
}
with
if (strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower() == "pdf")
{
   Response.ContentType = "application/PDF";
}
else
   if (strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower() == "doc")
   {
      Response.ContentType = "application/msword";
   }
   else
      if(strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower()== "xls")
      {
         Response.ContentType = "application/vnd.ms-excel";
       }
  else if (strFileName.Substring(strFileName.IndexOf('.') + 1).ToLower() == "ppt")
       {
           Response.ContentType = "application/vnd.ms-powerpoint";
       }

of course you can use switch statement, send the extension in the URL, use any other file types, but you should always match the extension with the contentType.

Nothing is impossible. The impossible only takes longer. "Digital Fortress, Dan Brown"
 
Old September 19th, 2008, 12:53 PM
Registered User
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Beholder
Default

That topic was quite useful! :D

I'd like to add some extra information:

To show a txt file:
Response.ContentType = "text/plain";

And to show a image

            byte[] blob = class.atBlob;
            FileStream fs = new FileStream("atBlob", FileMode.OpenOrCreate);
            fs.Write(blob, 0, blob.Length);
            Bitmap myBitmap = new Bitmap(fs);
            fs.Close();
            Char[] integer = { ',', '.' };
            int width = Int32.Parse(myBitmap.PhysicalDimension.Width.ToStr ing().TrimEnd(integer));
            int height = Int32.Parse(myBitmap.PhysicalDimension.Height.ToSt ring().TrimEnd(integer));
            Bitmap myThumbNail = new Bitmap(myBitmap, width, height);
            Response.ContentType = "image/jpeg";
            myThumbNail.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);


if you do intent to open a notepad file or an image in thy page, you should take it in consideration.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Word to Pdf Convertor c#.net Web Application aveekb ASP.NET 1.0 and 1.1 Professional 2 July 24th, 2013 04:11 AM
open pdf file within aspx page shaktis General .NET 3 November 2nd, 2007 12:58 PM
open a pdf file viren_balaut J2EE 0 January 26th, 2004 10:50 PM
open a pdf file viren_balaut Servlets 0 January 24th, 2004 01:03 AM
open a pdf file in browser viren_balaut Pro JSP 0 January 12th, 2004 01:27 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.