I'd like to use html+css+js to create an interface for setting up a simulation. The interface is used to create a visualisation of the simulation, and an output parameters file, by using several input files.
This web-page does NOT need to run on a server and is only run locally on the same machine.
I understand why a sever page is not allowed to access a client file, and that a server page may access another server file. Is it possible for a LOCAL web-page to access a local file?
It is possible to view the contents of any file using file:///path/to/file.txt so I presume that it is somehow possible to pull the result into a running page in the form of a string. However I'm having very little success.
If it is possible please could you detail a simple example of how the contents of a txt file could be read into a string?
Thank you very much
node.js is a javascript implementation that can easily do such things because it is a process running on the machine. A web page is running in a browser which does not have access to the file system, so that would not be possible, however you could bundle your web application into an electron application which would be able to access the osand file system, so while it would use html and javascript it would still require an installation.
If you are using Chrome, you can launch it with the --allow-file-access-from-files flag. Then you can do:
fetch('file:///path/to/file.txt')
.then(function(response) {
// do something with it
})
Related
I created a prototype about a tool that would analyze source code and write metadata about it in files to be read by a web application served locally.
Now I need to specify details about how I'll implement it. The data would need to go both ways, first the backend write to file and the frontend read from file, second the frontend write to file and backend read from file.
My question is about how I do the second part, saving from the browser to local files, knowing that I will be serving the page locally.
I've found this api https://web.dev/native-file-system/ that might be what I need, but it's pretty new and requires permission from the user. I guess I can do it differently because I'll serve it locally.
For more context, here's the video of my prototype
I am working on an application that should take file system path as directory and then further processing is done on that path. The code is written into javascript. I do no want to use Node.js or any external library for validation.
We just want to give path : C:/Users/Desktop/Test_Folder
How can we validate that path in js?
We need the solution to be worked on Windows, Linux and Mac OS.
If this is just an application in browser, then, generally, it is not possible. See the following questions:
Local file access with javascript
https://stackoverflow.com/questions/24369131/is-html5-file-api-dead
List local directory on Web Application
There was an attempt of a standardized file system API, which was discontinued. Seems like it only worked in Chrome anyway.
Update
A quote from the Chrome documentation:
Use the chrome.fileSystem API to create, read, navigate, and write to the user's local file system. With this API, Chrome Apps can read and write to a user-selected location. For example, a text editor app can use the API to read and write local documents. All failures are notified via chrome.runtime.lastError.
I am working on project for desktop application. I am using Qt controls with visual c++.
I am loading an html file in the QWebView as,
m_pWebView->load(QUrl("../../../demo/index_Splash_Screen.html"));
Now, what i want is, say, I have some .zip files in my location "c:\demo", I want list (or array of file names) of the files present in that directory.
How can i do this through javascript ?
PS: I went through this link, but it didnt match my requirement. I have not worked with of html, javascript and jquery. Please help me.
I'm afraid you cannot access local files or directories using javascript due to security issues.
Edit: I hadn't thought about the file api so thought for a moment this might not be true, but without some user input to give permission, this still cannot be done.
This question has a good response from PhilNicholas:
I'm afraid I may be the bearer of bad news for your design: The action
you are requesting expressly violates the security model as specified
in the File API spec. The client implementation of FileReader() must
make sure that "all files that are being read by FileReader objects
have first been selected by the user." (W3C File API , 13. Security
Considerations: http://www.w3.org/TR/FileAPI/#security-discussion).
It would be a huge security risk of browser scripts could just
arbitrarily open and read any file from a path without any user
interaction. No browser manufacturer would allow unfettered access to
the entire file system like that.
Thinking about it however, if it is all being run locally, you could use ajax to query a server side script that could return the directory you request.
If it is a Windows application then you could access the local filesystem by using ActiveX objects. You might have a look at this link Reading a txt file from Javascript
Note that activeX usage is possible only when using IE as browser/engine; I used to need it a while ago for developing an HTML application (.hta files).
I'm testing some of the new JS filesystem abilities, i.e. creating an empty text file in the local filesystem. I'm running the HTML & JS files from a local path (file:///). For this purpose I launched Google Chrome with the --allow-file-access-from-files flag from the CLI. The filesystem request is PERSISTENT (and works).
I have read up on different posts about the filesystem, copied and modified some of the code in the tutorials; When I launch the HTML file, my custom success/ failure messages are outputted in the console;
This is the result:
Opened file system:/ // this is the root path of the JS Filesystem.
/wtf.txt // this is the name and path of the text file I created+ it's a success
However, when I look at my directory's (both system and application root), there's no .txt file with the name I assigned to it. How can I know where Javascript really wrote this file? In what "root" (since the 'root' cannot be assigned)? What does it mean that the FileSystem is a 'sandbox'? That I cannot access the (virtual?) contents of it on my local drive, but only with JS? If this is the case, is there a way to prompt the user to save the file?
Thanks in advance for your answers
It seems you're expecting the File System API to work locally similar to an OS file system. The client doesn't work like that. In fact, and API is designed to be your interface, as a programmer, to the files and directories -- the client itself (e.g., Chrome, etc.) will handle the rest on the local level. The API is not designed by which you can create a file via the browser and easily access it via the operating system.
How can I know where Javascript really wrote this file? In what "root" (since the 'root' cannot be assigned)?
Technically speaking, each client can store locally as it chooses. So while you can go to the local file system to look for the file, something is wrong with your approach if you're attempting to do so; the File System API is not meant for that. To your question, you can assume that if there's content the client's storage area (e.g., for Chrome it's something like "C:\Users\USERNAME\AppData\Local\Google\Chrome\User Data\Default\File System\") then you can assume that the JavaScript wrote it. But again, it's not set up for user friendly browsing on the local system.
What does it mean that the FileSystem is a 'sandbox'?
Sandbox simply means an area created and set aside for a specific purpose, outside of which the client cannot see/access. See this from Mozilla: https://developer.mozilla.org/en-US/docs/WebGuide/API/File_System/Introduction#virtual
That I cannot access the (virtual?) contents of it on my local drive, but only with JS?
That is correct, and by design.
If this is the case, is there a way to prompt the user to save the file?
If I understand your question right, you're asking if there is a way to provide a specific file to the user and have it prompt them to save it locally. Well, of course if you provide a link to the file (or push it, a different discussion) then the client will prompt the user to save/store it if their platform allows them to do so. But you have no control over where they save it locally nor can you later get it it. If I've misunderstood your question, comment below and I'll follow up.
I'm trying to access a text file using javascript code, but I keep receiving a security error message. What I'm trying to do is:
var file = File('path/filename.txt');
The path is relative to the script location. I found that running from local machine could be the reason for that, so I tried to run from my local webserver but the error is still there. Does someone know why? What can I do to load this text file? If possible, considering that it is always under the script path, could it be also loaded from local disk (no web server)?
Thank you.
If you're trying to access a file on the server, you should do it using a server side coding language like PhP.
If you're trying to access a client file (= a file on the computer of your website visitor), it's not possible for obvious security reasons [EDIT] Thanks to Colin DeClue , I discovered this is possible using the HTML5 File API. An article explaining this is available here : http://www.html5rocks.com/en/tutorials/file/dndfiles/
Your snippet will attempt to load the file from your local file system, so can't retrieve a file from a server. You'd need to look at requestFileSystem or FileReader to make something like this work if the file is local.
Alternatively, you could use AJAX to retrieve the file from a server.