0

In our ASP MVC 3 site, we have some hyperlinks that point towards files out on our LAN (this is an intranet site). These links have ALWAYS worked up until today.. at which point not a single one will work. Prior to today you were able to click on the link and the file (usually an .xlsx spreadsheet) would open up.

The file path is saved as a field in the database. On the Index view, if this attachment field has content then a hyperlink with a paperclip image is used in this table cell. Here is the code from the view:

    <td>
        @if (!String.IsNullOrWhiteSpace(item.Attachment))
        {
            <a href=@item.Attachment class="noLine"><img src="@Url.Content("~/Content/Images/attachment.png")" height="20px;" width="20px;"/></a>
        }            
    </td>

And here is an example of how this HTML is rendered

<td>
    <a href="\\Prdhilfs03\l&amp;i-sales&amp;mkt\WORKAREA\Agencyservices\Shared\AIC\MasterListAttachments\AF.xls" class="noLine"></a>            
    <img src="/Content/Images/attachment.png" height="20px;" width="20px;">
</td>

If I right-click and copy the link address this is what I get on my clipboard

file://Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

However, if I right click the link, select "Inspect Element", then right click the anchor link from the console and select "Copy Link Address" I get this:

http://localhost:2574///Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls

Any help or suggestions would be appreciated. Thanks!

EDIT

When I click the link, I receive this error message in the Chrome console:

Not allowed to load local resource: file://Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls
NealR
  • 10,189
  • 61
  • 159
  • 299

3 Answers3

0

Did you try using forward slashes instead. I mean instead of

<a href="\\Prdhilfs03\l&amp;i-sales&amp;mkt\WORKAREA\Agencyservices\Shared\AIC\MasterListAttachments\AF.xls" class="noLine"></a>

to use

<a href="//Prdhilfs03/l&amp;i-sales&amp;mkt/WORKAREA/Agencyservices/Shared/AIC/MasterListAttachments/AF.xls" class="noLine"></a>

Convert the urls:

var links = document.querySelectorAll("a");
for(var i=0; i<links.length; i++) {
    var link = links[i];
    var url = link.getAttribute("href");
    if(url) {
        url = url.replace(/\\/g, "/");
        link.setAttribute("href", url);
    }
}
Krasimir
  • 13,306
  • 3
  • 40
  • 55
  • How would I specify that? The anchor link is in a loop so the `@item.attachment` is Razor that renders the link as I posted it. – NealR Sep 05 '13 at 21:54
  • I don't gave experience with Razor, but if if you could add some javascript code you may translate the urls. – Krasimir Sep 05 '13 at 21:57
  • Still getting the `http:` in front of the url. Added this command `url = url.replace("http:", "");` and getting the same thing. – NealR Sep 06 '13 at 17:02
0

First of all, prefix your href with "file://" instead of "\\", and this will fix the discrepancy between the two links.

Second, I believe that your problem has to do with cross site scripting. Your browser doesn't want to be tricked into downloading files from another server (I'm assuming that \\Prdhilfs03 is a different computer than the web server).

To verify this problem using Chrome, click on the link while your javascript console is open, and see if you get an error that says something like "Not allowed to load local resource:".

EDIT

OK, as verified by your error message, your web browser is preventing you from downloading a file from a different server to protect against cross site scripting. There is an article here that talks about steps to fix this by changing your chrome settings, although another option would be to store the files locally on the web server.

Community
  • 1
  • 1
Karl Wenzel
  • 2,412
  • 25
  • 24
0

I'll add to Karl's answer that you are running into the cross site scripting issue. An alternative you have is to have a controller that returns the file in question as a FileResult. Let's call this a FileRouter. The FileRouter will then have one action called Download, something like this:

public FileContentResult Download(string path)
{
    return this.File(/* a stream to the path */);
}

You would then pass the paths to the action on your controller. So instead of a direct link, you'll place an Html.Action link with a target of intranetserver/filerouter/download?path=path_to_file

Of course this has the drawback of the server loading the file off the network unnecessarily, but it's an alternative to consider.

Additionally, using a FilePathResult may work here as well, but I don't have any experience with it so can't make any claims on its efficacy here.

Ameen
  • 2,576
  • 1
  • 14
  • 17
  • It's a small group of users, so the network usage wouldn't be a problem. We did want to try and open up the actual file, though, as opposed to downloading a copy. I've never used this technique before, do you know if it accomplishes that or would this simply download a copy of the file? – NealR Sep 11 '13 at 21:25
  • The FileContentResult will download a copy unfortunately, so not what you actually want. :( The FilePathResult though may work. It might be worth putting a quick test on a local MVC project on your box to see the effects. Sorry, I originally missed the fact that you want the file directly opened. – Ameen Sep 12 '13 at 15:37