Category: Java Script

Convert local path to UNC (Universal) File Path using Java Script ActiveXObject


<script>

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

var WshNetwork = new ActiveXObject("WScript.Network");
var Drives = WshNetwork.EnumNetworkDrives();
for (i = 0; i < Drives.length; i += 2) {
if(Drives.Item(i) != "")
{
if(filePath.match("^"+Drives.Item(i))){
filePath = filePath.replace(Drives.Item(i), Drives.Item(i + 1));
break;
}
}}
}

</script>

<form onsubmit="getUNCPath()">
<input type="file" id="uploadedFile"/>
<input type="submit" value="Get the UNC Path!" />
</form>

in razor view engine add the following line to support ActiveXObject:

@using System.Web.Script.Serialization

%d bloggers like this: