Link to Directory on Network

11,755

Solution 1

This is more of a comment than an answer, but I can't comment...

This isn't an asp.net-mvc question. It's a browser question. Once the HTML is rendered, it doesn't matter if it was hardcoded, .net, or cgi. Don't hate the asp.net...

With that being said, I don't think anybody else can help you out if you cross post over there.

Considering your tests, it most definitely sounds like a security issue. And that makes perfect sense, too.

Plus, with some googling, I found this:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/HTML/Q_20405367.html

At least in IE (see the response from MS below), it's a specific security setting. I'd imagine something similar in FF and the others.

The issue you are encountering is a new security feature in Internet Explore 6 Service Pack 1. In order to prevent Internet vicious codes from accessing your local files, the development team developed the new security feature and included it in IE6 SP1. Please be advised that this is a normal behavior.

Actually, IE6 SP1 includes new security code checks that prevent "zone elevation". This means that documents located in the "Internet" zone will not be granted access (through HREFs, scripting...etc) to documents in the "My Computer" zone. Only documents located in the "Trusted" or "My Computer" zone can access documents in the "My Computer" zone.

Therefore, if you want to allow untrusted documents to access documents in the "My Computer" zone, you might add the source URL in "Trusted sites" (this must be done with caution for obvious security reasons). The "Local Intranet" zone is trusted as well.

Furthermore, the following registry key allows disabling the new security codes check that prevent "zone elevation": - You can create this DWORD key and set it zero to disable this new feature. Also, you can enable it by changing it to 1 at any time.

HKCU\Software\Microsoft\Internet Explorer\Main\Disable_Local_Machine_Navigate = 0 REG_DWORD

It is not recommended to use above registry key since it breaks the "zone elevation" security fix.

James

Solution 2

I was just working on a similar problem and came up with the following solution. I'm using ASP.NET but not ASP.NET MVC; however, from what I know and what I read, this should also work for ASP.NET MVC. First, create your link like this:

<a ID="A1" runat="server">Documents</a>

Then in the codebehind (assuming codebehind is in C#), in Page_Load set up a click event:

A1.ServerClick += A1_ServerClick;

And have a click event like this:

protected void A1_ServerClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("C:\\ruby");
}

System.Diagnostics.Process.Start will simply open a directory. To avoid issues with the directory not existing, you can check for existence with Directory.Exists("C:\ruby") and create the directory if it does not exist with Directory.CreateDirectory("C:\ruby")

Solution 3

Probably some security restriction in the browser. Did you try other browsers?

Solution 4

I also tried it its not working your way. Why dont you try this way, File Upload Download, here you can use the download section and while download it will ask you if you want to open it.

Solution 5

Perhaps there's an issue with redirecting from IE to a folder. Try perhaps redirecting to a local file using file:/// - does this work:

 <a href="file:///C:\ruby.html">Document</a>

?

If it does, try perhaps sticking a trailing slash to your URL:

 <a href="file:///C:\ruby\">Documents</a>

Or use backslashes:

 <a href="file:///C:/ruby/">Documents</a>
Share:
11,755
thebrokencube
Author by

thebrokencube

Updated on August 23, 2022

Comments

  • thebrokencube
    thebrokencube over 1 year

    So I'm working on an Intranet web application using ASP.NET MVC, and I need to create a link to a folder on the network. So, as an example, let's say I wanted to create a link to "C:\", and this uri is stored in site.DocsPath. I figured the easiest way to do this was just create a regular link, like this:

    <a href="file:///<%= site.DocsPath %>">Documents</a>
    

    This resolves to:

    <a href="file:///C:\ruby">Documents</a>
    

    However, when you click on the link, nothing happens. It's basically like clicking on regular text; absolutely nothing happens. No redirection, nothing. I tried this in both Firefox and IE, and this same behavior happens in both.

    Initially I thought it might be the slashes. So I pasted file:///C:\ruby into the address bar, to see if it was even right. It worked. I tried this in both Firefox and IE, and it works in both.

    So now, I'm thinking "hey, maybe my html isn't proper for some reason". So I created a small html page, as such:

    <html>
    <head><title>Test Page</title></head>
    <body>
        <a href="file:///C:\ruby">Documents</a>
    </body>
    </html>
    

    And lo and behold..... it worked. I click on the link, and it actually follows the link. And this works in both Firefox and IE.

    So now I'm confused. The HTML is exactly the same in both cases (through ASP.NET MVC and in static HTML). And yet it only works in the static HTML case.


    Now, I'm just pulling at straws. I try just pasting

    <a href="file:///C:\ruby">Documents</a>
    

    straight into the ViewPage in ASP.NET MVC. Nope, doesn't work.

    Then I try pasting just a random website statically into the ViewPage, like:

    <a href="http://www.google.com">Supreme Overlord of the Internet</a>
    

    And that works. So, now I have confirmed that ASP.NET actually can follow hand-generated links.

    Now, with just nothing left to do, I do something crazy. I set the link to somewhere that doesn't exist, like:

    <a href="file:///X:\this\doesnt\exist">I Hate ASP.NET MVC right now</a>
    

    Firefox sticks to its guns and doesn't follow it. However, IE actually follows it and gives me an error page. The same thing happens if site.DocsPath = "X:\this\doesnt\exist" and I put:

    <a href="file:///<%= site.DocsPath %>">Documents</a>
    




    So now, I'm totally confused. I don't know what the heck is going on. Clearly, ASP.NET MVC hates me, which is troubling because I have shown it nothing but love.

    If anyone has any idea what is going on, I would greatly appreciate the help. Thanks!


    UPDATE: After much testing (and many very helpful answers and comments from everyone here at SO), I've come to the conclusion that just creating a normal link to the folder just will not work. I eventually tried putting that static html page I created above on a webserver, and it turns out that it doesn't work. I also created a Ruby on Rails application and a small PHP application and tried it through those, and it doesn't work on them either. So the only other possibility is that it is in fact a browser thing.

    I think I will pursue maybe somehow connecting to the SharePoint server that all the documents are managed by. Thanks to everyone who commented and provided various answers on the question. I can only pick one answer, but everyone's comments and answers really provided a clear picture as to what was going on. Thanks!

  • thebrokencube
    thebrokencube almost 15 years
    Hmm... I tried that, and it didn't work =/. It does the same thing as what I tried. But yea, I know that this won't work properly on the web. I'll clarify what i'm trying to do in my original post. Thanks!
  • thebrokencube
    thebrokencube almost 15 years
    Yea, I tried it in both Firefox and IE. I'll clarify my problem a bit more in my original post. Thanks!
  • Kevin Gauthier
    Kevin Gauthier almost 15 years
    I think this is by design, as a security feature.
  • thebrokencube
    thebrokencube almost 15 years
    Well, I have gotten it to work elsewhere. It works in a static html page, and i've also gotten it to work in rails before by just doing it the intuitive way.
  • Kevin Gauthier
    Kevin Gauthier almost 15 years
    Your browser does not treat HTML differently because it comes from Rails or MVC. Identical HTML will be treated the same. OTOH, it might well treat a local file differently than one on a web server, even a local one.
  • thebrokencube
    thebrokencube almost 15 years
    Yea, that makes sense. I've added in another test case that addresses this point. Still lost as to why this is happening though =/.
  • San
    San almost 15 years
    Actually it looks complicated but its very simple, and one more thing the code there is using BinaryContentResult as the return type and he has derived it from the ActionResult, but the current version of ASP.NET MVC has FilePathResult as the return type for the action, it makes it very simple.
  • thebrokencube
    thebrokencube almost 15 years
    Correct me if i'm wrong, but it seems that the solution presented in that blog post seems to actually "retrieve" those files so you can list them on your website, while all I want to do is let the browser handle it. i.e. if you just typed in something like C:\ into the Firefox address bar, it would deal with it (similarly, IE would just open a windows explorer window).
  • thebrokencube
    thebrokencube almost 15 years
    I tried just linking to a normal file, but it doesn't work. As I noted in my question, if I take a link that WORKS in static html and copy it exactly as is into the viewpage, it DOESN'T WORK. Like, the link exists, but when you click on it, absolutely nothing happens. No redirect, no error page, nothing.
  • synhershko
    synhershko almost 15 years
    Yes, I understood what you were asking. The link will work if the file exists, and you're clicking this link on the same computer that contains that file. Also, it only works if the page with the link is also opened locally. If served by a webserver (local or not), it won't work. Perhaps you could still make it work by making that site trusted in your browser, but I'm not 100% sure that'll work.
  • San
    San almost 15 years
    Yes its actually retrieves the files, its not to do with the browser. Initially it will display all the file names as links, on the website, once u click on the link it will give u the option to save or open the file.
  • thebrokencube
    thebrokencube almost 15 years
    Ah, okay. Well the reason that won't work for what I'm trying to do is because I don't want to actually download the files. The option with opening the file still saves that file to your temp folder before opening it, so it's not actually opening the file from the original location.
  • thebrokencube
    thebrokencube almost 15 years
    Yea, i have no idea how to make the site trusted in my browser (or even how that would help). At this point I'm pretty much under the impression that there's something I need to configure on the webserver so that it allows following local links.
  • synhershko
    synhershko almost 15 years
    No, there is nothing you can do at the webserver level. This is solely a security issue on the browser end that prevents "live" web pages from posing a local links to the user. As far as I can tell, this is by design. And here's how to make a site trusted on IE: marshallgis.org/marshallgis/trustedsite/…
  • thebrokencube
    thebrokencube almost 15 years
    Lol, I don't actually hate ASP.NET... I was just somewhat frustrated with this problem when I wrote the post. I tried doing this with Rails, and I got the same error, so I guess it is a browser thing. However, it's still really weird that it allows static html to access local files. Anyways, thank you for including an actual reference to the problem.
  • Pieter le Roux
    Pieter le Roux almost 15 years
    I don't think it allows "static html"... it probably allows a local file to access a local file. Try serving that through your webserver. It makes sense that a file:// can point to another file (local web browsing from a cd for example) but not http:// -> file://. .......... I realise you don't hate mvc ;) If only I had saved all my testing code with "why the f**** isn't this working?", "f****************ck!", etc
  • thebrokencube
    thebrokencube almost 15 years
    You're right; I tested it on a webserver and it didn't work either. I guess I'll just have to bark up another tree to get this functionality to work. Thanks for the help!
  • turbo88
    turbo88 almost 6 years
    This works locally. But it does not work when you deploy the solution on the server.