Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open local file in browser

Website has tag, where href is path to local file. For example, <a href="D:\test.txt">Link</a>. It doesn't work.

How to do it right? :) It must work in IE only, other browsers are not necessary

like image 280
Denis Sokolov Avatar asked Mar 20 '14 13:03

Denis Sokolov


People also ask

How do I open a local folder in my browser?

While in the browser, press Ctrl+O in Windows (Cmd+O on Mac) as in “Open” and double-click the appropriate file.

How do I open a file in my browser?

Opening a Local File on Chrome is quite simple, you just have to open a New Tab, hit Ctrl+O, go to your file's location, select and open it. There are different types of files one can open in Chrome Browser such as Text, video files, . mp3 files, images, etc.

How do I make a file open in browser instead of downloading?

To make certain file types OPEN on your computer, instead of Chrome Downloading... You have to download the file type once, then right after that download, look at the status bar at the bottom of the browser. Click the arrow next to that file and choose "always open files of this type". DONE.


2 Answers

Here you need to use a "file" protocol to link a file in the HTML like,

<a href="file:///D:\test.txt">Link</a>

The browser may or may not open the file due to the security setting. You can click the right button and choose "copy link address" and then paste it into the browser.

like image 124
Kungfu_panda Avatar answered Sep 18 '22 15:09

Kungfu_panda


I came across the same problem and looked for a solution but couldn't find anything, file:// was saying not allowed to open local file and this wouldn't be possible to work

May not solve it for everybody but I did a workaround in php using file_get_contents(). It's a function in php that gets the text content of a file and puts it into a variable - which does allow you to access local files on a network.

So you just make a php file that takes the id or string of the file and pulls the data and use php to put it onto the screen so as far as the browser is concerned its a hosted file

<a href='getContents.php?id=5'>

getContents.php

<?php
$id = $_GET['id'];
if(ctype_alnum($id)){//
//get file name from Database
$data[5]['file_path'] = 'file.htm';
$page = file_get_contents('//server//'.$data[$id]['file_path']);
echo $page;
}
?>
like image 45
Bim Avatar answered Sep 18 '22 15:09

Bim