How do I read/check if a file in the same folder of the page exists, when I open the html page locally. The page is made for local use only.
Asked
Active
Viewed 57 times
-4
-
2Possible duplicate of [Access Local Files with Local Javascript](https://stackoverflow.com/questions/1903959/access-local-files-with-local-javascript) – Joseph Sible-Reinstate Monica Mar 28 '18 at 11:20
-
use node.js with `fs` (file system) and existsSync or stat https://nodejs.org/api/fs.html#fs_fs_stat_path_callback – George Mar 28 '18 at 11:21
1 Answers
-3
try making an ajax request to retrieve the file. Example with jQuery:
$.get('url to your local file')
.done(()=>{ alert('file exists'); })
.fail(()=>{alert('file cannot be retrieved');});

Eriks Klotins
- 4,042
- 1
- 12
- 26
-
-
if the file actually exists, make sure the url is correct and you run your script and access the file from the same domain. e.g. http://localhost/ – Eriks Klotins Mar 28 '18 at 12:36
-
As said, the file is opened locally, so there is no domain. It is offline. – ThePat02 Mar 28 '18 at 12:37
-
see here: https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local You may need to install a local server for such functionality – Eriks Klotins Mar 28 '18 at 12:44