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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
EspavoCommented:
What happens if you use a <asp:hyperlink />?
Then:
HyperLink1.NavigateUrl=("~/Manuals" & file2.Name)
Espavo
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DStarkeyAuthor Commented:
Thanks for the help.....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.