How to open local file in browser

63,509

Solution 1

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.

Solution 2

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;
}
?>

Solution 3

Use a file URI.

Can’t test it (have no Windows/IE), but it should be:

file:///D:/test.txt

See also:

Solution 4

if you want to open the files local with the default program, than you can check my url protocol https://github.com/berti92/FileBridge

Install it and create a link with the url protocol

<a href="filebridge:PASTE HERE THE PATH">Open me</a>
Share:
63,509
Denis Sokolov
Author by

Denis Sokolov

Updated on July 09, 2022

Comments

  • Denis Sokolov
    Denis Sokolov almost 2 years

    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

  • Tony Brix
    Tony Brix about 9 years
    You should also set the Content-Type header if the file is something other than HTML