Open a pdf document in Javascript

I have a folder in my local system (Ex: D://SamplePdfs/) which contains only pdf documents.I am using bootstrap / angularjs (1.5) / html5 ( frameworks / technologies for UI. In the UI I will have list of hyperlinks. My requirement is when user clicks on hyperlink (ex:sample1) , it should search for a filename (ex: sample1.pdf) in the folder located in my local system (D://SamplePdfs/) and then if hyperlink name and the pdf filename matches then the pdf doc should open in a new tab/window.Otherwise “Not Found” message should be shown.

I want some sample code how to implement this using Javascript / Angularjs.

Ex:

sample1    (hyperlink) -->  D://SamplePdfs/sample1.pdf
     
sample12   (hyperlink) -->  D://SamplePdfs/sample2.pdf
 
foo        (hyperlink) -->  "Not Found" message. Since foo.pdf not there in the folder

Any help would be appreciated.

Sounds pretty much like regular target="_blank" behaviour to me…

<a href="/SamplePdfs/sample1.pdf" target="_blank">sample1</a>

Now this will not display the error message in the current window, but then again I guess it would make sense to only show links for files that actually exist. ;-) You can’t read a directory on the server with JS running in the browser though, so you’d have to set up an endpoint to list the files inside /SamplePdfs/, which you could then request using AJAX (naturally $http) and render accordingly in an ng-for loop.

But just to clarify, you can’t link to files on your local file system, the files would of course have to be on the actual server… presumably something like /var/www/SamplePdfs/. I’m not familiar with windows servers though, so please forgive me if this is obvious. :-)

1 Like

@m3g4p0p Thank you so much for the explanation. It is very helpful.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.