How can I create a link to a local file on a locally-run web page?

后端 未结 5 928
野性不改
野性不改 2020-11-22 03:15

I\'d like to have an html file that organizes certain files scattered throughout my hard drive. For example, I have two files that I would link to:

  • C:\\P
相关标签:
5条回答
  • 2020-11-22 03:33

    back to 2017:

    use URL.createObjectURL( file ) to create local link to file system that user select;

    don't forgot to free memory by using URL.revokeObjectURL()

    0 讨论(0)
  • 2020-11-22 03:34

    You need to use the file:/// protocol (yes, that's three slashes) 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 which I'll cover in the last section. If it opens, it will only ever open in the browser. If your browser can display the file, it will, otherwise it will probably ask you if you want to download the file.

    Modern versions of many browsers (e.g. Firefox and Chrome) will refuse to cross from the http protocol to the file protocol to prevent malicious behaviour. You'll need to open your webpage locally using the file protocol if you want to do this stuff at all.

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

    The first part of a URL is the protocol. A protocol is a few letters, then a colon and two slashes. HTTP:// and FTP:// are valid protocols; C:/ isn't and I'm pretty sure it doesn't even properly resemble one.

    C:/ also isn't a valid web address. The browser could assume it's meant to be http://c/ with a blank port specified, but that's going to fail.

    Your browser may not assume it's referring to a local file. It has little reason to make that assumption because public sites usually don't attempt to link to peoples' local files.

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

    Why three slashes?

    Because it's part of the File URI scheme. You have the option of specifying a host after the first two slashes. If you skip specifying a host it will just assume you're referring to a file on your own PC. This means file:///C:/etc is a shortcut for file://localhost/C:/etc.

    These files will still open in your browser and that is good

    Your browser will respond to these files the same way they'd respond to the same file anywhere on the internet. These files will not open in your default file handler (e.g. MS Word or VLC Media Player), and you will not be able to do anything like ask File Explorer to open the file's location.

    This is an extremely good thing for your security.

    Sites in your browser cannot interact with your operating system very well. If a good site could tell your machine to open lecture.mp4 in VLC.exe, a malicious site could tell it to open virus.bat in CMD.exe. Or it could just tell your machine to run a few Uninstall.exe files or open File Explorer a million times.

    This may not be convenient for you, but HTML and browser security weren't really designed for what you're doing. If you want to be able to open lecture.mp4 in VLC.exe consider writing a desktop application instead.

    0 讨论(0)
  • 2020-11-22 03:45

    Janky at best

    <a href="file://///server/folders/x/x/filename.ext">right click </a></td>
    

    and then right click, select "copy location" option, and then paste into url.

    0 讨论(0)
  • 2020-11-22 03:47

    If you are running IIS on your PC you can add the directory that you are trying to reach as a Virtual Directory. To do this you right-click on your Site in ISS and press "Add Virtual Directory". Name the virtual folder. Point the virtual folder to your folder location on your local PC. You also have to supply credentials that has privileges to access the specific folder eg. HOSTNAME\username and password. After that you can access the file in the virtual folder as any other file on your site.

    http://sitename.com/virtual_folder_name/filename.fileextension

    By the way, this also works with Chrome that otherwise does not accept the file-protocol file://

    Hope this helps someone :)

    0 讨论(0)
  • 2020-11-22 03:47

    I've a way and work like this:

    <'a href="FOLDER_PATH" target="_explorer.exe">Link Text<'/a>
    
    0 讨论(0)
提交回复
热议问题