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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.