0

I have read this post 1 and the answer was very helpful. I have also read post 2 because I have a similar implementation which was helpful too.

I created two buttons elements using JavaScript which is supposed to navigate/open a folder in the browser (similar to how it's advised to do in Post1 above).

This is how my buttons inside my list-group-items looks like in my page:

My page

The container above is populated by this JS function upon a successful AJAX response (similar to post 2 link above):

function upon_success(data) {        
    event.preventDefault();
    //.. 
    let button1 = '<button class="btn btn-sm btn-primary my-2 my-sm-0 teButton" type="submit" value='+args[1]+'><i class="fa fa-download"></i> TE Log </button>';
    let button2 = '<button class="btn btn-sm btn-primary my-2 my-sm-0 ueButton" type="submit" value='+args[2]+'><i class="fa fa-download"></i> UE Log </button>';
    $("#bottomContainer").append('<a class="list-group-item list-group-item-light py-0 ">'+'Test case : '+args[0]+'<span class="pull-right">'+button1+'  '+button2+'</span></a>');
    //..
}

this.value below has a network path and already has two // on it. I only need to add 1 more /. Right now it doesn't open a network path or even a local directory on the browser.

// jQuery for button 1. This is supposed to open a folder in browser where the files are.
$(document).on("click",".teButton",function (){
    var log1 = "file:/"+this.value;
    document.getElementById("debug").innerHTML = log1 // FOR DEBUG PRINTING.
    // window.location.href = log1 // Why?
    // window.location.href = 'file:///C:\TestPC\Log_Database\Lab'
    // window.location.href = 'file:///C:\TestPC\Log_Database\Lab\something.txt'
    window.location.href = 'file:///C:\Utils'
});

//jQuery for button 2.
$(document).on("click",".ueButton",function (){
    var log2 = "file:/"+this.value;
    window.location.href = log2
});

Here is my js.fiddle

I have two questions:

  1. Why isn't my browser opening the folder path/tree when I press/click the button. (If you look at the image I have attached above, you could see the path I am trying to open, which I have printed it. I used file:/// with 3 backslashes)

The error from console says: "Not allowed to load local resource: file:///C:/Dropbox"

  1. How do I actually download a single file when I click on a button? This is a separate need for another button. (should go to Downloads folder of browser)
  • Browser will not give you an access to files, you need server to do that for you. – Just code Dec 14 '18 at 04:54
  • Not that this would fix your problem, but notice also, that the paths are off, the backslash is an escape character in JS, and e.g. the first out-commented path is actually "file:///C:TestPCLog_DatabaseLab". – Teemu Dec 14 '18 at 05:18
  • @Justcode: Not sure what you mean, all I need is it to open the folder in the browser like this: https://imgur.com/a/rz8Qge2 – YouGotA.BigEgo Dec 14 '18 at 06:34
  • @Teemu: Ok, I will give you that, but the uncommented path, is the local directory and it should've opened it like this image in the browser: https://imgur.com/a/rz8Qge2 according to the answer in post1 I have linked. – YouGotA.BigEgo Dec 14 '18 at 06:38
  • No, browsers use URLs, not paths. You can use the backslash too, but in a literal JS string it has to be escaped, i.e. doubled. It's just much simpler to use a slash instead. You can open/load local files/folders only when you're hosting your page locally, i.e. when you're using file schema. – Teemu Dec 14 '18 at 06:40
  • are you getting any console errors? your jsfiddle seems broken – Just code Dec 14 '18 at 06:41
  • @Justcode: I am getting a console error, which I have posted above. Also my jsfiddle is not broken, it won't run because there is no backend to a response to the AJAX function. It's only written to show you what I have done, to help describe my problem. The list-group-items of my container is not populated with my jsfiddle. But I have shown an image of how it would look like when it works on my station. – YouGotA.BigEgo Dec 14 '18 at 06:45
  • `"Not allowed to load local resource: file:///C:/Dropbox"` will not work because you don't have any access to local files as I said read this https://stackoverflow.com/questions/14052473/go-to-local-url-with-javascript – Just code Dec 14 '18 at 06:46
  • @Teemu: I am only trying to do what is advised to do this post in my question in this thread: https://stackoverflow.com/questions/18246053/how-can-i-create-a-link-to-a-local-file-on-a-locally-run-web-page – YouGotA.BigEgo Dec 14 '18 at 06:46
  • See the difference, the examples are in HTML, not in JS. And they are running the page on a local system, not via web. Now please try to understand this, you can't access user's local drive from a web server programmatically. The only way to do this is to use the [FileReader API](https://developer.mozilla.org/en-US/docs/Web/API/FileReader), when the user himself will select the file to open. – Teemu Dec 14 '18 at 06:52
  • Ok, I won't use a local drive, I used it only because my django development server is running on the local system. Is it possible to open a network path or any path? How are people able to download files from the internet? That is my question. Since I have a folder, I wanted it to open that path in the browser as user @doppelgreener said in the other post. I can't be the first person trying to do this. – YouGotA.BigEgo Dec 14 '18 at 06:56
  • The files to download from the internet are stored at public web servers, they are not stored in users' local machines ... If you have a local server, just save the files somewhere inside the document root, or configure the server to get more rights on your local system. Notice, that a local server is still a server, and uses http(s) protocol, accessing local files using file protocol violates cross-origin policy. – Teemu Dec 14 '18 at 07:03
  • The problem I have is, I have 10s of thousands of files, and many of them are 50MB or higher. I can't store that many files in document root. I want to offer users the ability to download a file or offer them the folder. Is that possible at-least? This is so common many sites do it all the time. I am only asking how do they accomplish that. My files are not in local directory. It's a network path. – YouGotA.BigEgo Dec 14 '18 at 20:09

0 Answers0