3

I am looking for a method for obtaining the 'real' UNC path from a mapped network drive - a common question here but not using the language or method that I desire.

I would like users to be able to select a file using an HTML form but not upload it (the files are enormous). All I want to obtain is the full UNC path to the file. The trouble is that users will typically select files out of their mapped network drive (e.g. g:\file.txt) which is of no use to me - I must have the full \\server\user\file.txt path.

I know that in Windows you can use net use at the command line for information about mapped drives, and you can also obtain the information from the HKEY_CURRENT_USER\Network\driveletter, but I need a method to obtain this from an HTML form.

My initial thought was some form of Javascript, but I doubt the registry key will be accessible in this way.

  • I doubt that this is possible without the use of a local installed component. Would it be thinkable of having users install something first? – rene Feb 23 '13 at 10:42
  • 1
    if this is on a corporate network then perhaps you could accept the filename as you get it (G:\file.txt) and then use some server-side code to connect back to the user's pc (you should easily be able to get their IP address) and get the list of network drive mappings to work out where the drive letter is assigned to? – paulH Feb 25 '13 at 16:00
  • @monkeymatrix: are you completely against a simple ActiveX control? – Mike Perrenoud Feb 27 '13 at 17:24
  • Does this need to be done on the client side? If you are using .Net I believe I have a possible solution. – jason Feb 28 '13 at 18:29

1 Answers1

4

You can do this really simply with a very concise piece of JavaScript and an already installed ActiveX control. Below is a complete web page that will allow you to browse for a file and then show you the full UNC path of the selected file in an alert:

<html> 
    <head> 
    <script type="text/javascript" language="JavaScript"> 
    if (typeof String.prototype.startsWith != 'function') {
      // see below for better implementation!
      String.prototype.startsWith = function (str){
        return this.indexOf(str) == 0;
      };
    }

    function getUNCPath() {
        var fileName = document.getElementById("uploadedFile").value;

        var WshNetwork = new ActiveXObject("WScript.Network"); 
        var Drives = WshNetwork.EnumNetworkDrives(); 
        for (i = 0; i < Drives.length; i += 2) {
            if (fileName.startsWith(Drives.Item(i))) {
                var fullPath = fileName.replace(Drives.Item(i), Drives.Item(i + 1));
                alert(fullPath);
                break;
            }
        }
    }
    </script> 
    </head> 
    <body> 
        <form onsubmit="getUNCPath()"> 
            <input type="file" id="uploadedFile"/>
            <input type="submit" value="Get the UNC Path!" /> 
        </form>
        <span id="uncPath"></span>
    </body> 
</html> 
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Thanks for the answer, however this doesn't appear to work on IE, and the major downside of ActiveX is that it's not cross-browser - I have no way to know what browser my users may use :( –  Feb 28 '13 at 10:42
  • 2
    You said it **doesn't** work in IE? It would in fact **only** work in IE. What version of IE are you using? Did you tell IE to allow blocked content and give it permission to run the ActiveX control? Also, you don't really expect to be able to gather proprietary client information from a machine via a ***boxed and secured*** JavaScript environment do you? – Mike Perrenoud Feb 28 '13 at 12:11
  • IE8, and there were no alerts, pop-ups, or anything else asking for permission. Thank you for your answer, but any solution would need to be cross-browser. I don't expect to be able to mine sensitive data from Javascript, but there may be some method for obtaining something as simple as 'where the file really is' on an upload form, hence the question! :) –  Feb 28 '13 at 12:16