Getting UNC path from mapped network drive

I'm looking for a way to get a "real" UNC path from a mapped network drive - a common question here, but not using the language or method I desire.

I would like users to be able to select a file using the HTML form, but not upload it (the files are huge). All I want to get is the full UNC path of the file. The problem is that users usually select files from their mapped network drive (eg g: \ file.txt) which is useless to me - I must have the full path \\server\user\file.txt

.

I know that on Windows you can use net use

the command line to get information about mapped drives and you can get information from as well HKEY_CURRENT_USER\Network\driveletter

, but I need a method to get this from an HTML form.

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

+3


source to share


1 answer


You can do this simply with a very concise piece of JavaScript and an ActiveX control already installed. Below is the complete web page that will allow you to view the file and then show the full UNC path of the selected file in 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> 

      

+4


source







All Articles