8

I'm using the following when trying to open a local file:

<a href="file:///Users/username/Dropbox/Documents/a/some document.numbers">some document</a>

When I click the above in a browser, it opens Finder to the folder. But does not open the file. Should I be doing something else to have the file open in Numbers?

4thSpace
  • 43,672
  • 97
  • 296
  • 475

7 Answers7

7

You cannot open local files on the client. This would be a huge security risk.

You can link to files on your server (like you did) or you can ask the client for a file using <input type="file">

SourceOverflow
  • 1,960
  • 1
  • 8
  • 22
4

You can only open some types of files in browsers, like html css js and mp4, otherwise the browser will want to download it. Also remember that browsers replace spaces with %20. I recommend right clicking the file and opening it with chrome then copy that link and using it.

You can open files that are local as long as it is a file that is on the file that is trying to open another file is local.

DrBrad
  • 305
  • 2
  • 13
  • How would that be easier than just providing the link in plain text? – SourceOverflow Sep 26 '17 at 15:56
  • Because the issue most likely lies in the url not linking correctly. However it may be the type of file. Changing the file to a .txt may fix this issue. – DrBrad Sep 26 '17 at 15:58
  • No, it is simply not allowed. There is no issue with the html code. You aren't allowed to open another program from a website. – SourceOverflow Sep 26 '17 at 15:59
  • If this is not a website and simply a file on the persons computer then you may open any file from any dir on the users pc. If he is trying to make a website then you are of course right, he cant unless he makes an extension that allows for that. – DrBrad Sep 26 '17 at 16:01
  • 1
    seems like you are right. Usually you would set up a local server if you need it for a program or similiar, where it again needs to be in the server files. But if you don't do that, you are indeed correct, I'm sorry – SourceOverflow Sep 26 '17 at 16:07
  • `%20` didn't make any difference. Still opens Finder to the location. This is my local computer so security isn't a concern. – 4thSpace Sep 26 '17 at 16:19
  • Okay, so go to the file, right click it hit open with and select a browser. It should open if not it will download. In the case that it downloads change the extension from a .numbers to something that the browser will recognize like .txt in the case that it works copy the url from the browser and use that one in place of the one you have for the file. – DrBrad Sep 26 '17 at 16:23
  • @DrBrad: I don't have any idea why I'd want to go through all of that. The whole reason for creating the HTML file was easily consolidate access various files scattered across my computer. Click a link and file opens. You are just creating more hoops to jump through. – 4thSpace Sep 26 '17 at 16:44
  • That is simply to get the link, once you have the link then you don't have to do that again. Your link is not working because its not a valid link. This will ensure the link is correct and you wont have to type it in again. – DrBrad Sep 26 '17 at 17:00
  • It bounces from the browser back to Finder. The browser URL is blank. – 4thSpace Sep 26 '17 at 17:53
  • Hmmm, would it be possible to change the location of you file. The issue may be that the file is in a location that isn't stored on that pc. – DrBrad Sep 26 '17 at 18:02
2

Your issue is likely the space in the document name. Try this instead:

<a href="file:///Users/username/Dropbox/Documents/a/some%20document.numbers">some document</a>

The %20 will be read by your browser as a space.

Update
The other answer points out something I missed. The .numbers extension will not be able to be opened directly by your browser. Additionally the other answer describes the security risk this could create.

Justin
  • 64
  • 4
2

The File API in HTML 5 now allows you to work with local files directly from JS (after basic user interaction in selecting the file(s), for security).

From the Mozilla File API docs:

"The File interface provides information about files and allows JavaScript in a web page to access their content.
File objects are generally retrieved from a FileList object returned as a result of a user selecting files using the <input> element, from a drag and drop operation's DataTransfer object, or from the mozGetAsFile() API on an HTMLCanvasElement."

For more info and code examples, see the sample demo linked from the same article.

brichins
  • 3,825
  • 2
  • 39
  • 60
1

This might not be what you're trying to do, but someone out there may find it helpful:

If you want to share a link (by email for example) to a network file you can do so like this:

file:///Volumes/SomeNetworkFolder/Path/To/file.html

This however also requires that the recipient connects to the network folder in finder --- in menu bar,

Go > Connect to Server

enter server address (e.g. file.yourdomain.com - "SomeNetworkFolder" will be inside this directory) and click Connect. Now the link above should work.

Matthew
  • 668
  • 5
  • 12
1

Here is the alternative way to download local file by client side and server side effort:

 <a onclick='fileClick(this)' href="file://C:/path/to/file/file.html"/>

js:

function fileClick(a) {

    var linkTag = a.href;
    var substring = "file:///";
    if (linkTag.includes(substring)) {
        var url = '/v/downloadLocalfile?path=' + 
     encodeURIComponent(linkTag);
        fileOpen(url);
    }
    else {
        window.open(linkTag, '_blank');
    }
}

function fileOpen(url) {
    $.ajax({
        url: url,
        complete: function (jqxhr, txt_status) {
            console.log("Complete: [ " + txt_status + " ] " + jqxhr);
            if (txt_status == 'success') {
                window.open(url, '_self');
            }
            else {
                alert("File not found[404]!");
            }
            // }
        }
    });
}

Server side[java]:

 @GetMapping("/v/downloadLocalfile")
  
  public void downloadLocalfile(@RequestParam String path, HttpServletResponse 
    response) throws IOException, JRException {
     try {
      String nPath = path.replace("file:///", "").trim();
      File file = new File(nPath);
      String fileName = file.getName();
    
      response.setHeader("Content-Disposition", "attachment;filename=" +  
       fileName);
      if (file.exists()) {
        FileInputStream in = new FileInputStream(file);
        response.setStatus(200);
        OutputStream out = response.getOutputStream();
        byte[] buffer = new byte[1024];

        int numBytesRead;
        while ((numBytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, numBytesRead);
        }
        // out.flush();
        in.close();
        out.close();
    }
    else {
        response.setStatus(404);
    }
  } catch (Exception ex) {
     logger.error(ex.getLocalizedMessage());
  }
return;
}
starball
  • 20,030
  • 7
  • 43
  • 238
Mayen
  • 331
  • 3
  • 6
0

You can expose your entire file system in your browser by using an http server.

caddy2 server

caddy file-server --listen :2022 --browse --root /
  • serves the root file system at http://localhost:2022/

python3 built-in server

python3 -m http.server
  • serves current dir on http://localhost:8000/

python2 built-in server

python3 -m SimpleHTTPServer
  • serves current dir on http://localhost:8000/

This s

ccpizza
  • 28,968
  • 18
  • 162
  • 169