Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Absolute path to a local file in the user computer

 
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there local file manipulation that's been done with JavaScript that can actually get the absolute path to a file or folder when the user chooses with a directory or file picker?

Specifically, I'd like to read/show the ABSOLUTE PATH from a file and open the file if the user requests it. At this point I'm not worried about gaining permissions and am just assuming I already have full permissions to these files.
 
Sheriff
Posts: 67619
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Security concerns prevent the browser from having such access to the file system.
 
Castiel Snow
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No. Security concerns prevent the browser from having such access to the file system.



Is there anyway to bypass that?
Extensions?
Java apps?
Anything?
Im using SpringBoot
 
Bear Bibeault
Sheriff
Posts: 67619
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It wouldn't be much of a security measure if it could be easily bypassed.

Spring Boot and other Java apps run on the server and have no access to the client machine.

What is it that you are specifically trying to accomplish? You may need to run an agent on the client (which of course requires the permission of, and actions by, the user).
 
Saloon Keeper
Posts: 7231
169
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While the JavaScript File API -which is available in most browsers- lets you access files that the user chooses, it deliberately hides the path information. It does allow you to work with the file contents, though, thus enabling some form of "opening the file" that you mentioned within the web app.

But if you need something like this, you may want to consider if a web app is really the right way to go, or if a desktop app might be better suited.
 
Master Rancher
Posts: 4599
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want just anything, here is a technque I have used to call a java program from a browser.  I normally use it through a bookmark on the browers' bookmark bar but it seems to work from an external html file:


<html>
<head>
<script type="text/javascript">
 document.location = 'D:/JavaDevelopment/runtime ExecuteJava.jar"';   //<<<< calls a java program
</script>
</head>
<body>
Will ExecJava work here?    YES it does!!!
</body>
</html>


It requires an entry in the Windows Registry that defines: ExecJava
 
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Node,js you can use the path resolve() function to get the absolute path to a file on the server. Don't expect that to work on a web page, however. As Bear has said, security (sandbox) constraints forbid a web page from directly accessing the file system. The closest you can get is via file upload/download, where you can pre-populate the file dialog box. But there the browser itself is responsible for reading/writing files and will only do so by explicit permission from the user.

A webapp server cannot actually access client files at all. They don't share the same file system (usually), so you can't just call the "open" function on a file path on a foreign computer. Web-based "files" are copies of data, not the actual data itself and that data is embedded within the request POST stream (for uploads) or response stream (for downloads). The browser is responsible for copying in the data for a POST and saving data for a download, Never client-side user logic.

That, incidentally, was one of the reasons ActiveX failed. Too much scope for mischief. Plus, of course, some of us don't use Windows.

Aside from the lack of direct filesystem access, it would be a Really Bad Idea to allow the server to control the client's computer. A bad-intentioned server could absolutely litter an unsuspecting client with virus files if that were allowed. For a similar reason, web pages cannot automatically print without approval from the user. Not only because of possible exploits via the printing system, but simply because otherwise I could spitefully print out page after page of garbage and waste paper and printer resources.

Incidentally, file uploads from Internet Explorer often contained the full client-side pathname as the "filename" parameter of the POST. That wasn't a good idea, since while the server cannot use the path directly, it does hint at where the user stores things, and the first law of securty is to never volunteer any more information than you have to. Most web clients uploaded only the file name without the path.



 
Castiel Snow
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please don't update an old post like that. Users will not notice the update after they have replied. I have reverted the edit. Please post a new post with the additional information.



Sorry, I thought was best to not pollute it. But I understand. Sorry.
Good thing I had a copy of this. Would be a pain to write all over lol

Im converting a DesktopApp(JavaFx) to a WebApp (Spring Boot).

The DesktopApp has a field called 'hyperlink', which is a String field. The User utilizes the JFileChooser to pick a file or even a folder and save the ABSOLUTE PATH to this file on the field called hyperlink. The user can also manually set the field and for example, set a internet URL (for example, a dropbox link with public access).

This field is then saved in the database.

Other users can then try to access this file. It usually works because users save the absolute path of the local network or even Internet URL's like 'https://redacted.something.com/file.txt.

In this case, a user could open the app, find this 'hyperlink' field and click open --> If its a URL, it will open a browser page. If its a network file, it will open explorer.exe on the absolute path given by the field.

However, this functionality must now exist on the spring boot version.

Problem

Was unknown to me that Browser's security forbid you to access local files. So now, I can neither get the absolute path with the HTML file chooser or access a local file with it.

How should I deal with this?

Someone mentioned me I should think about using either an extension in browser OR a java plugin (ew?).
 
Tim Holloway
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best bet is to not catalog files under someone's local file pathname to begin with. "Usually" isn't a word I like to hear next to "works" anyway.

You cannot escape the sandbox using plugins and even if you could, you'd never sell it on the open Internet. Java applets could relax sandbox rules enough to do that, but applets had so many security issues that they should be considered as dead technology.

It sounds like you aren't actually up/downloading files, just their local pathnames. Not really a net-friendly scheme when all's said and done. Probably better done with a standard Java Application (which can access the local filesystem) or just leave it in JavaFX.
 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

A possible use case where a web app needs to access the file system is when you want to upload a file. You would let the user select a file from the local file system using the HTML input tag of type file, like this:

<!DOCTYPE html>
 <html lang="en">
 <head>
   <title>Testing File Upload</title>
<script src="myscripts.js"></script>
 </head>
 <body>
<input id="file-upload" type="file"  
     accept=".gif,.jpg,.jpeg,.png">
 </body>
</html>

I hope this will help you
This HTML alone will let the user browse the file system and select a file. When the user selects the file, the following javascript code in myscripts.jar will convert the selected file into data URL. Data URL is a Base64 encoded version of the selected file.

myscripts.js

window.addEventListener("load", function() {
 document.getElementById("file-upload").onchange = function(event) {
   var reader = new FileReader();
   reader.readAsDataURL(event.srcElement.files[0]);
   var me = this;
   reader.onload = function () {
     var fileContent = reader.result;
 console.log(fileContent);
   }
}});
You can store the value of file content in a database and then allow the user to download the file display the file on the browser if the file is an image or pdf.

 
Tim Holloway
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To reiterate: A webapp can only access files on the web server. It cannot access files on the web client (browser). HTTP is not a file-sharing protocol. Nor does HTTP act as a file server. A web client can send data to a server, but it cannot write directly to the server's filesystem. If the receiving application wishes, uploaded file data can be altered or even discarded and may or may never be actually stored in a server-side file.

An HTML form can request for a user to select a file on their local filesystem (NOT the server's filesystem) via a file-browser dialog and if the user does so and submits the form, the contents of that file will be MIME-encoded and wrapped in the uploaded HTTP Request stream. The server cannot read the file directly.

Likewise, if a server wishes to download data, it can wrap in in MIME encoding and send it out as part of the HTTP Response stream. However, what happens when the client (browser) receives that response is defined by the client's own custom MIME rules, which are typically settable as browser options. An HTTP client may opt to save the data as a file, in which case a local file dialog will be presented with the user being given the option as to where (or if) the data will be used to create/update a file. Or the MIME rule may simply say "display content in browser". The server can suggest, but it cannot control and it can never force a file to be saved without the client's explicit. permission.

MIME, incidentally, stands for Mail Internet Media Encoding and, as its name implies was actually originally intended to send data attachments in emails in a form that would allow the data to route and render even if the source and deistination (and possibly intermediate relays) were completely different in word size, byte ordering, character sets, and so forth.
reply
    Bookmark Topic Watch Topic
  • New Topic