0

Is there a way to open a PDF file from a remote server directory inside a gridview using NavigateUrl? I'm able to retrieve the file number from the textbox and generate the link, but when you click on the link nothing happens. Could my NavigateUrl be formatted wrong due it being on a remote server and trying to open in a web browser? My code below. Thanks in advance.

            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" 
                        NavigateUrl='<%# Eval("Name", "file:\\fileserver\pdf\{0}") %>' 
                        Target="_blank" Text='<%# Eval("Name", "{0:d}") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" DataFormatString="{0:d}" HeaderText="FILE NUMBER" />
            <asp:BoundField DataField="CreationTime" DataFormatString="{0:d}" HeaderText="DATE ADDED" />
            <asp:BoundField DataField="Length" DataFormatString="{0:#,### bytes}" HeaderText="FILE SIZE" />
g_shockTan
  • 325
  • 2
  • 8
  • 15

2 Answers2

1

The navigate url needs to look like a URL not a physical file path. somthing like http://server/path/x.pdf. If you own the share where the documents are stored consider adding a virtual directory that points at the remote server and directory. After that it would be possible to access the files from your asp.net application by using http://yourserver/virtualpath/x.pdf

Mike Beeler
  • 4,081
  • 2
  • 29
  • 44
  • Thanks. Creating a virtual directory in IIS. Will let you know. – g_shockTan Jan 18 '13 at 16:58
  • Great news. Took your advise and created a virtual directory sub folder and changed the NavigateUrl to ='<%# Eval("Name", "/fileserver/pdf/{0}") %>'. Refreashed the browser, clicked on the file number link and a new tab opened showing the pdf file. Problem solved. Is there anything else I need to correct? – g_shockTan Jan 18 '13 at 17:30
0

You will need to have the file accessible via http:// and not file:// in order for the the link to work properly.

Once that is set, use the Server.MapPath() method to generate the URL based on the path to your file.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • Thanks. I've changed it to NavigateUrl='<%#Server.MapPath(Eval("Name", "http:// fileserver/pdf/{0}")) %>' and now I get "not a valid virtual path" error. How would I make the pdf files accessable via http? – g_shockTan Jan 18 '13 at 16:53
  • There are two things to check: First that the folder in question has read priviliges by the ASP.Net Worker Process and Network Service (the two services that typically run your web server) Second: You can always copy your PDF files into a directory within the root of your website. – Dillie-O Jan 18 '13 at 16:58
  • Thanks for your help. Creating a virtual directory to make the files accessbile via http:// resolved the issue. – g_shockTan Jan 18 '13 at 17:32
  • Sounds good. Make sure to upvote and mark it as the answer. 8^D Keep the SO community strong. – Dillie-O Jan 18 '13 at 17:33