Denis Sokolov
Denis Sokolov

Reputation: 301

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

Upvotes: 8

Views: 66470

Answers (4)

Berti92
Berti92

Reputation: 471

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>

Upvotes: 1

Kungfu_panda
Kungfu_panda

Reputation: 421

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.

Upvotes: 7

Bim
Bim

Reputation: 31

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

Upvotes: 3

unor
unor

Reputation: 96577

Use a file URI.

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

file:///D:/test.txt

See also:

Upvotes: 2

Related Questions