Solved

Open .pdf file through the browser

Posted on 2004-08-21
10
301 Views
Last Modified: 2010-05-18
I went through this site, step by step, and all it does it clear the screen and put a small image icon in the top left side of the screen.  The one that shows if there is a missing image.  I have adobe acrobat reader, and opened the file fine.  I didn't see any "Adobe Acrobat" items in the references list to be added... (if that is the issue)?

http://support.microsoft.com/default.aspx?scid=kb;en-us;307603&Product=vbNET

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Response.ContentType = "Application/pdf"
        'Get the physical path to the file.
        Dim FilePath As String = MapPath("readme.pdf")
        'Write the file directly to the HTTP output stream.
        Response.WriteFile(FilePath)
        Response.End()
    End Sub

Any clue?  Thanks!
0
Comment
Question by:sticar
  • 6
  • 2
  • 2
10 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11861884
are you stuck some where ? this code is working fine, it is opening the file...  What is your exact problem ?
-Baan
0
 

Author Comment

by:sticar
ID: 11862426
Strange...  all it does for me is put a little square at the top left, but the link stays the same (which it should).

I don't have to include any resources do I?
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11862942
are you sure the FilePath variable is pointing to the correct path or the physical file is in the folder?
use File.Exists( FilePath ) method to determine whether the file is there.
0
 

Author Comment

by:sticar
ID: 11863769
I went through debugger and it has the full correct path.  It acts like it doesn't see it though.  This may not matter, but the icon for "readme.pdf" is a generic one, not the Adobe Acrobat one, but it will open up in Adobe when I right-click and open it.
0
 

Author Comment

by:sticar
ID: 11863801
I just upgraded my Adobe Reader to 6.0.2 from 5.0 and it opens just fine now... ugh.....

Thanks anyway!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sticar
ID: 11863855
If you know how I can modify it to open in a separate window, I'll give you the points.

Thanks for all your help!
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11864649
Hi there,
say you are clicking on a link to open that. In the Page_Load event of the newly opened window, you have the working code. Now go to the parent page (where the link is) and use Javascript:window.open('MyNewPage.aspx')..  just like I showed for the Camtasia Files.
0
 

Author Comment

by:sticar
ID: 11865504
? I'm working with a combo box actually so when I change the item it immediately opens the link window.

        If strExt = ".pdf" Then
            Response.ContentType = "Application/pdf"
            'Get the physical path to the file.
            Dim FilePath As String = MapPath(dir + filename)
            'Write the file directly to the HTTP output stream.
            Response.WriteFile(FilePath)                                        'can I put javascript in here somehow?
            Response.End()
        End If
0
 
LVL 20

Accepted Solution

by:
ihenry earned 250 total points
ID: 11868665
Try this quick sample, the code is quite self explanatory.

=-=-=- webform3.aspx =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="webVB.WebForm3"%>
<HTML>
      <HEAD>
            <title></title>
            <script language="javascript">
            <!--
            function OpenFile( sel, useCodebehind )
            {
                  if (sel.selectedIndex == 0) return;

                  //var text = sel.options[sel.selectedIndex].text
                  var file = sel.options[sel.selectedIndex].value
                  if ( !useCodebehind )
                  {
                        //window.open(file,'newWin','width=400,height=400,toolbar=1,menubar=1,location=1,resizable=1')
                        window.open(file,'newWin','width=400,height=400,resizable=1')
                  }
                  else
                  {
                        //window.open('WebForm4.aspx?f=' + file,'newWin','width=400,height=400,toolbar=1,menubar=1,location=1,resizable=1')
                        window.open('WebForm4.aspx?f=' + file,'newWin','width=400,height=400,resizable=1')
                  }
            }
            // -->
            </script>
      </HEAD>
      <body>
            <form id="Form1" method="post" runat="server">
                  <p>
                        <h4>Open a file directly from JavaScript</h4>
                        <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
                  </p>
                  <p>
                        <h4>Open a file from code behind</h4>
                        <asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
                  </p>
            </form>
      </body>
</HTML>
=-=-=- webform3.aspx.vb =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Public Class WebForm3
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DropDownList2 As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then

            DropDownList1.Items.Add(New ListItem("-- Select a file to open --", "value0"))
            DropDownList1.Items.Add(New ListItem("DateTime Structure FAQ", "docs/datetimefaq.pdf"))
            DropDownList1.Items.Add(New ListItem("How to Solve Debugger Problems", "docs/howtosolvedebuggerproblems.pdf"))
            DropDownList1.Items.Add(New ListItem("Journal on Wild Web West", "docs/journal1_web.pdf"))
            DropDownList1.Items.Add(New ListItem("Design of a Persistence Layer White Paper", "docs/persistenceLayer.pdf"))

            DropDownList2.Items.Add(New ListItem("-- Select a file to open --", "value0"))
            DropDownList2.Items.Add(New ListItem("DateTime Structure FAQ", "datetimefaq.pdf"))
            DropDownList2.Items.Add(New ListItem("How to Solve Debugger Problems", "howtosolvedebuggerproblems.pdf"))
            DropDownList2.Items.Add(New ListItem("Journal on Wild Web West", "journal1_web.pdf"))
            DropDownList2.Items.Add(New ListItem("Design of a Persistence Layer White Paper", "persistenceLayer.pdf"))

        End If

        DropDownList1.Attributes.Add("onchange", "JavaScript:OpenFile( this, false )")
        DropDownList2.Attributes.Add("onchange", "JavaScript:OpenFile( this, true )")
    End Sub

End Class
=-=-=- webform4.aspx =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm4.aspx.vb" Inherits="webVB.WebForm4"%>
<HTML>
      <HEAD>
            <title></title>
      </HEAD>
      <body>
            <form id="Form1" method="post" runat="server">
                  &nbsp;
            </form>
      </body>
</HTML>
=-=-=- webform4.aspx.vb =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-Public Class WebForm4
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            If Not IsNothing(Request.QueryString("f")) Then
                Dim f As String = Request.QueryString("f")
                Dim filePath As String = MapPath(f)
                Response.ContentType = "application/pdf"
                Response.WriteFile(filePath)
                Response.End()
            End If
        End If
    End Sub

End Class


Hope this helps
0
 

Author Comment

by:sticar
ID: 11877261
That is PERFECT!!  I used dropdownlist1
Thank you!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now