Vb.Net Open .PDF file onClick

I have been trying to open a .pdf file using a linkbutton. Basically when the button is clicked I want to open the selected .pdf file. I will be passing in the file name but for now I just hard coded for testing.  I  think I am close but I need a little help.

Thanks
Dim sdir As New DirectoryInfo(MapPath("~/Manuals/"))
        Dim file As FileInfo() = sdir.GetFiles()

        For Each file2 As FileInfo In file

            If file2.Name = "Test_Manual.pdf" Then
                Response.Write(file2.Name)
            End If

        Next

Open in new window

DStarkeyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
EspavoConnect With a Mentor Commented:
What happens if you use a <asp:hyperlink />?
Then:
HyperLink1.NavigateUrl=("~/Manuals" & file2.Name)
Espavo
0
 
masterpassCommented:
try this , its really simple
Private Sub displayPDF(ByVal docName As String)
    Try
        Response.ClearContent()
        Response.ClearHeaders()
        Response.ContentType = "application/pdf"
        Dim reportsPath As String = "reports path here"
        Response.WriteFile(reportsPath + docName)
        Response.Flush()
        Response.Close()
    Catch ex As Exception
        Throw ex
    End Try
End Sub

Open in new window

0
 
AbarajCommented:
you can try this using simple HTML
<a href = "Test_Manual.pdf" target="_blank">Test_Manual.pdf</a>
by clicking the like, it  will ask you to save / open the file. you can open this. :)
 
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
DStarkeyAuthor Commented:
Thanks for the response masterpass.

Nothing happens when I run the code. Should I import a specific namespace or do I need anything special in IIS for this to run?


 Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
  
        displayPDF("Test_Manual.pdf")


    End Sub
    Private Sub displayPDF(ByVal docName As String)
        Try
            Response.ClearContent()
            Response.ClearHeaders()
            Response.ContentType = "application/pdf"
            Dim reportsPath As String = "~/Manuals/"
            Response.WriteFile(reportsPath + docName)
            Response.Flush()
            Response.Close()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Open in new window

0
 
EspavoCommented:
Are you working in a Web-Application?
If yes.
  • Then all you need is a Hyperlink pointing to the pdf file
  • Be aware that the way that PDF will open will be dependant on the user having a PDF Reader installed, and the way it opens will depend on how the user has configured the reader..
BTW, what masterpass is doing is sending a pdf in a stream to the browser... which doesn't seem (to me) to be what you are asking...
Espavo
 
0
 
masterpassCommented:
Use MapPath("~/Manuals/") instead of just "~/Manuals/"
0
 
DStarkeyAuthor Commented:
Yes I am using a web application...

I tried the MapPath and still no good.

Let me give a little more explaination:
I will be displaying a hyperlink or button in a listview which when clicked will display the correct .pdf file depending on the record id within the listview. The DB holds the file name, which the file will reside in the manuals folder within the web application.

I am using Telerik Control which uses an Item Command that I will be using to handle the click event and at the same time get the files name for display.

Thanks to everyone so far for there repsonses. I hope this explains my situation a little better.
 
0
 
EspavoCommented:
If you have a Valid URL it should work...
What error are you getting? (What is the actual URL that you are generating?)
What happens if you do something like:
Response.Write("<a href=""file2.name"">" & File2.name & "</a>")
Remember to add the path in the href...
Espavo

0
 
DStarkeyAuthor Commented:
Espavo,
When i add the url to the linkbutton using the resolveurl in the code behind nothing happens. If I use a hyperlink and add the path statically within the control it works fine. IF I try to add the path to the hyperlink in the code behind I get the following message "Propery must assign to the property or use its value".
I got to be making this harder then it should be....

I do get all the files back using the code below. When the IF statement = true it tries to run the statements.
Both the Respose and LinkButton1 do not get the .pdf to popup.


  Dim sdir As New DirectoryInfo(MapPath("~/Manuals/"))
        Dim file As FileInfo() = sdir.GetFiles()

        For Each file2 As FileInfo In file

            If file2.Name = "Test_Manual.pdf" Then
                'Response.Write("<a href=""~/Manuals/"">" & file2.Name & "</a>")
                LinkButton1.ResolveUrl("~/Manuals/" & file2.Name)
            End If

        Next

Open in new window

0
 
DStarkeyAuthor Commented:
Thanks for the help.....
0
All Courses

From novice to tech pro — start learning today.