Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerson/cb2da58243837b41b4aaa4541a2380f3 to your computer and use it in GitHub Desktop.
Save cemerson/cb2da58243837b41b4aaa4541a2380f3 to your computer and use it in GitHub Desktop.
Workaround for being unable to view a local or shared drive file:/// in a browser
<!DOCTYPE html>
<html lang="en">
<!--
Very basic workaround for being unable to view local/network file:/// links in Chrome
Referencde: https://stackoverflow.com/questions/28724751/open-local-filesfile-using-chrome
To use load this file in a browser with a querystring appended w/your local or shared drive file path. Example:
https://mywebsite.com/ThisPage.html?sdfurl=C:\Documents\MyDocument.pdf
or
https://mywebsite.com/ThisPage.html?sdfurl=Z:\Resources\PDFs\MyFile2023.pdf
-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Local or Network File</title>
</head>
<style>h1{color:#ccc; text-align:center; } body {padding-top: 4%; color: #444; text-align: center; width: 100%; margin: 0px auto; max-width: 1150px; } *{font-family:sans-serif; } h2 {border: 1px solid #537484; border-radius: 4px; padding: 5px 30px; font-size: 12px; font-weight: normal; font-style: italic; color: #537484; background: #ddf2fa; margin-top: -15px; display: inline-block; min-width: 590px; margin: 4px auto 15px; } a#sdfurl, a#sdfpath, input[type="text"] {padding: 10px 10px; border: 1px solid #aaa; display: block; max-width: 330px; text-align: center; border-radius: 5px; margin: 5px auto; } span#lbFolder, span#lbFile {font-weight:bold; color:#444; border:1px solid #ddd; padding:3px 7px; border-radius:4px; display:block; width:500px; margin:2px auto; } p:nth-child(2), #lbFile{font-size:19px; } p{color:#888; } p:nth-child(3), span#lbFolder{font-size:13px; font-weight:normal; } </style>
<body>
<h1>Local or Network File</h1>
<p>File: <span id="lbFile"></span></p>
<p>Folder: <span id="lbFolder"></span></p>
<a href="#" onclick="showSDFURL()" id="sdfurl">View File in Browser (if possible)</a>
<br/>
<a href="#" onclick="showSDFPath()" id="sdfpath">View Network File Path</a>
<script>
var sharedDriveFileCheck = getSharedDriveFileURL();
function getShareDriveFilePath(){
var sharedDriveFilePath = '';
var urlContainsSharedDriveFilePath = (document.location.search.indexOf('sdfurl=',0)>-1);
if(!urlContainsSharedDriveFilePath){
console.log('Sorry no shared drie file path provided.');
}else{
sharedDriveFilePath = document.location.search.split('?sdfurl=')[1];
sharedDriveFilePath = sharedDriveFilePath.split('/').join('\\');
}
console.log('getShareDriveFilePath() = [' + sharedDriveFilePath + ']');
return sharedDriveFilePath;
}
function getSDFFolder(){
var folder = '';
try{
var path = getShareDriveFilePath();
var folderParts = path.split('\\');
folder = '';
for(var i=0;i<folderParts.length;i++){
if(i<folderParts.length-1){
folder = folder + folderParts[i] + '\\';
}
}
}catch(ex){
}
return folder;
}
function getSDFFile(){
var file = '';
try{
var path = getShareDriveFilePath();
var folderParts = path.split('\\');
file = folderParts[folderParts.length-1];
return file;
}catch(ex){
}
return file;
}
function showSDFURL(){
try{
var sfURL = getSharedDriveFileURL();
if(sfURL != ''){
prompt('Shared/Network Path File URL:\n\nCopy/paste this URL into your browser address bar to view the file:\n(You must have access to the path/drive for the link to work)',sfURL);
}else{
console.log('not redirecting.');
}
}catch(ex){
}
}
function showSDFPath(){
var sfPath = getShareDriveFilePath();
if(sfPath != ''){
prompt('Shared/Network File Path:\n\nCopy/paste this path into a Windows Explorer (or compatible) file browser to view the file:',sfPath);
}else{
console.log('not redirecting.');
}
}
function getSharedDriveFileURL(){
var sharedDriveFileURL = '';
var urlContainsSharedDriveFilePath = (document.location.search.indexOf('sdfurl=',0)>-1);
if(!urlContainsSharedDriveFilePath){
console.log('Sorry no shared drie file path provided.');
}else{
sharedDriveFileURL = document.location.search.split('?sdfurl=')[1];
sharedDriveFileURL = 'file:///' + sharedDriveFileURL.split('\\').join('/');
}
console.log('getSharedDriveFileURL() = [' + sharedDriveFileURL + ']');
return sharedDriveFileURL;
}
function redirectToSharedDriveFile(){
console.log('redirectToSharedDriveFile()...');
try{
var sfURL = getSharedDriveFileURL();
if(sfURL != ''){
document.getElementById('lbFolder').innerText = getSDFFolder();
document.getElementById('lbFile').innerText = getSDFFile();
}else{
console.log('not redirecting.');
}
}catch(ex){
}
}
window.setTimeout(function(){
redirectToSharedDriveFile();
},100);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment