Open local file from browser

Asked

Viewed 1,023 times

2

Hello,

What is the most correct way to open a local file (network-shared drive) through the browser? The file must be opened through a local application (desktop) previously installed on the PC. Example: Link to an excel file, when clicking should open the file in Excel(Program)

<a href="file:///C:\Users\jsantos1991\Desktop\Teste.xlsx">Excel</a>

The point is that this way appears the dialog asking if you want to open or save and if by chance the option save generates a copy of the file.

How I can open through the local app?

inserir a descrição da imagem aqui

According to my research what I need is a URI scheme ms-excel:

However I can open the window to start application but then generates me the following error:

inserir a descrição da imagem aqui

Thank you

2 answers

2

You need to use the protocol file:/// (yes, it’s three bars) if you want to link to local files.

<a href="file:///C:\Programs\sort.mw"> Link 1 </a>
<a href="file:///C:\Videos\lecture.mp4"> Link 2 </a>

These will never open the file in your local applications automatically.

That’s for security reasons, and I’ll get to that at last section. If it opens, it will only open in the browser. If your browser can display the file, it will. If you can’t, probably will ask if you want to download.

Some browsers, such as modern versions of Chrome, refuse to go from http protocol to file protocol. Therefore, it is better open this file locally using the file protocol, if you want to do these things.

Why does he get stuck without file: ///?

The first part of a URL is the protocol. A protocol is a few letters, then two dots and two bars. HTTP:// and FTP:// are valid protocols; C:/ it’s not and I’m sure it’s not even looks like a.

C:/ is also not a valid web address. The browser can assume that he must be http://c/ with a blank door specified, but this will fail.

Your browser may not assume that you are referring to a file site. There are few reasons to make this assumption because the sites public generally do not try to link to local archives of people.

So if you want to access local files: tell to use the file protocol.

Why three bars?

Because it’s part of the scheme URI of the archive. You have the option of specify a host after the first two bars. If you ignore the specification of a host, it will only assume that you are if referring to a file on your own PC. In other words: file:///C:/ etc is a shortcut to file:// localhost/C:/etc.

These files will still open in your browser and that’s good

Your browser will respond to these files in the same way as they responded to the same file anywhere on the Internet. These files will not be opened in its standard file handler (for example, MS Word or VLC Media Player), and you will not be able do nothing like ask File Explorer to open the location of filing cabinet.

This is extremely good for your safety.

Sites in your browser cannot interact with your system very well operational. If a good site could say.mp4 lecture to open in VLC.exe, a malicious website could tell you to open virus.bat on CMD.exe. Or you can just tell your PC to run some Uninstall.exe files, or open the File explorer one million times.

This may not be convenient for you, but HTML and security browser is not really designed for what you are doing. If you want to open Lecture.mp4 on VLC.exe, consider writing a desktop app.

0

The "most correct" form is relative, it depends on the language you are using.

For example, if you want to use Html5, you can use the code below as part of upload functionality:

<input type="file" name="arquivos" class="btn btn-success"/>

Remembering that this input only loads the information from where the file is on the user’s machine, you still need to upload / send that file to the server, if that’s what you want.

If you cannot use Html5, but are using some web language/framework (like java with JSF, Struts, Spring...) you can use some framework scriptlet.

If you want to do without a supporting server language, you can search for file upload scripts, in javascript with or without jquery (javascript file upload / jquery file upload).

  • Hello Reinaldo, notice that I want to open the link with a local application. Example an Excel file, I want to create a link that will open that file with Excel itself already installed on PC, do not want to upload documents.

  • 1

    I understand, and you are already doing as you should, "file:///C: 1 2 3 file.xlsx". The issue now is the security of browsers, Nowadays browsers try to block the interaction with the operating system.

  • This link (in English) explains in detail why the browser will not let you open an xlsx as if it were a shortcut to microsoft excel, for example. https://stackoverflow.com/a/18246357/9691138

Browser other questions tagged

You are not signed in. Login or sign up in order to post.