1

I'm trying to create a hyperlink to a pdf stored on our network drive. The problem is that I create the hyperlink through ASP.NET but clicking the link does absolutely nothing. If you inspect the element and click the link it works just fine.

It seems to be related to this issue on HTML links to local network shares.

By downloading the extension for Chrome referenced in that thread I am able to open local links. In Internet Explorer I am able to click the links without any issue or extensions.

I populate the NavigateNurl through a configuration value:
System.Net.WebUtility.HtmlEncode(configValue)

Here is the path I am using (with slight alterations in name):
file://unk-file01/forms%20engine/Templates/Fake/ES_User_Manual_GBA_FINAL_081514.pdf

<div id="manualMenu" runat="server">
  <div id="manual">
    <asp:HyperLink ID="ESUserManualLink" Target="_blank" runat="server"  Text="GBA User Manual" >
      GBA User Manual
    </asp:HyperLink>

    <asp:HyperLink ID="UnderwritingManualLink" Target="_blank" runat="server" 
                   Text="GBA Underwriting Guide">
      GBA Underwriting Guide
    </asp:HyperLink>

  </div>
</div>

Is there anything else I can do to make this work across all platforms besides hosting the files elsewhere?

KyleMit
  • 30,350
  • 66
  • 462
  • 664
mortey
  • 179
  • 1
  • 4
  • 15

1 Answers1

3

If you can make your URLs look like http://example.com/DownloadPdf.ashx?file=ES_User_Manual_GBA_FINAL_081514 then suitable code-behind for DownloadPdf.ashx could look like

Imports System.IO
Imports System.Web
Imports System.Web.Services

Public Class DownloadPdf
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim fname = context.Request.QueryString("file")
        Dim actualFile = Path.Combine("//unk-file01/forms engine/Templates/Fake", fname) & ".pdf"

        If File.Exists(actualFile) Then
            context.Response.ContentType = "application/pdf"
            context.Response.AddHeader("Content-Disposition", "attachment; filename=""" & Path.GetFileName(actualFile) & """")
            context.Response.TransmitFile(actualFile)
        Else
            context.Response.Clear()
            context.Response.TrySkipIisCustomErrors = True
            context.Response.StatusCode = 404
            context.Response.Write("<html><head><title>404 - File not found</title><style>body {font-family: sans-serif;}</style></head><body><h1>404 - File not found</h1><p>Sorry, that file is not available .</p></body></html>")
            context.Response.End()

        End If

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Note that it forces the file to have an extension of .pdf - you could add additional security such as preventing directory traversal and checking the user is allowed to access the file.

Code not tested as-is, but the code it is derived from works in a production environment (where it does have more security).

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • as of now I cannot change the URL to have HTTP or host it anywhere other than our local server. I was able to make it work with non-local files (such as espn) – mortey Sep 03 '14 at 20:59
  • @mortey Oh yes you can :) Just take the `configValue` you refer to in your question and extract the relevant parts to construct something which can be used in a URL parameter as a reference to the required file. – Andrew Morton Sep 03 '14 at 21:05
  • @mortey ... or are you saying that the server with the PDF files on it cannot be accessed by the web server? In that case, you would need to have the PDF files on the web server. – Andrew Morton Sep 04 '14 at 22:31
  • So I am very new to this career, my first job, and apparently those files will be hosted elsewhere when we shift to production. No one had told me that so I had assumed it would stay on a local server...now thinking about it it doesn't make sense but I didn't know. Thanks for your help Andrew! – mortey Sep 05 '14 at 12:23