How can I get list of filenames from a specified folder through javascript?
Assuming the folder you're talking about is on the client, then you can't. It'd bring up all sorts of security issues if JavaScript had that power.
If the folder is on the server then you could use some form of AJAX, with the server side code fetching the files in the specified directory and returning it back to the client.
Related
So i have set up a basic nodejs server using express and everything works fine. I have a json file in my folder that i want to be able to modify, to add some data or remove some.
What is the best way to do that without the user being able to interact with the actual file? Perhaps a database that i could send my data to?
I am very new to nodejs and javascript so anything that is as simple as possible is the best.
You could create different http routes for setting and fetching data in the json file.
Say, for example:
GET /data would read the json file and respond with the contents of the file.
POST /data could post the contents of the file.
PUT /data/key could be used to modify the contents of one single key in the json.
That being said, this looks like something that you should be using a DB for. If somewhere down the line, you choose to dockerize your app, everytime you restart your server, your JSON file would be reset to the initial config.
To avoid that, a db could be used. Mongo is a good place to start considering your choice of language and nature of your data.
Hope you find this helpful :)
I have a server-side subfolder structure in my HTML5/JS site.
The subfolder structure contains various media types where each media file is wrapped in its own HTML file which contains metatags.
I want to list all metatags for all files but I do not want to have to browse for a file (i.e. no FileSystem API) and get it's metadata. I just want to scan through the subfolder and list all metadata in each file.
I'm not able to find any script to do this, everything I keep running into is asking for the FileSystem API and the requirement to browse for a file.
alternatively, if FileSystem API can do this, I'd use it as long as I don't have to go browsing for files to use it.
My server is a standard LAMP server and the files are all HTML files inside a site subfolder. This site currently has no DB and I'm hoping to not add one for this functionality.
Any help would be appreciated.
Maybe Node.js would be a good fit for you. Then you can write everything in Javascript. It is server side scripting, but for demonstration purposes the configuration is much easier than Apache.
If I understand correctly that you don't want to browse the file system on the server side, but on the client side you are willing to do anything with Javascript, then the following may also be an option.
Using LAMP, you can configure Apache so that it shows directory indexes (https://httpd.apache.org/docs/current/mod/mod_dir.html#directoryindex), which you can use to browse to the content you need on the client side.
In any case, you will have to hit the files on your server somehow, either directly through the file system on the server side or with an HTTP request from the client side.
I have director structure like this, only $(Root Directory)'s name is known at run time.
other folders and files are generated dynamically.(All files are .json)
my requirement is I need to count no of files and read content of all files using jquery and ajax.
I know if we have some static path like abc/xyz/somefile.json then we can read someFile.json but in my case I need to traverse nested folders.
help
You will not be able to do it without server-side support, either from the web server itself or another server-side process.
If directory listing is enabled on your web server, you can make an ajax request directly to the directory you want and scrape the returned document's content.
Another way would be to setup a web service which allows to query a directory's content. That service would be responsible for querying the file system and return the information to the client in a data-interchange format like JSON.
My application creates .xml files and stores them on the user's hard drive; there is a default folder that I set in the web.xml to store the files, let's say D:/temp. Now after I write the files I need to read them back with javascript, I am using a javascript library that has this function mxUtils.load('URL of the file') (this function returns the content of the file), the problem is that it is giving me an error Cross origin requests are only supported for HTTP, (I now it doesn't have anything to do with the function or the library) I think the problem is that you can't read local files because of some security issues. Anyone can advise me some solution? Thanks
You cant access local filesystem using javascript.
For accessing the file using javascript , you have to upload it to a server and access it using the files url.
As stated, you cannot access a file on the filesystem strictly through Javascript. You could, however, use the input file type to upload the file to your server and then read it:
<input type="file" name="myfileinput">
Then, you can access it via the $_FILES global in PHP - other languages also provide this functionality through other means. Please note, again, that there is absolutely no way to access a file that is on someone's filesystem with Javascript without their consent (i.e. using the file input type). That would be a huge security risk - imagine going to a page and having it wipe your whole D:/ drive.
It's best to expose your files via HTTP and use mxUtils.load("http://yoursite/static/yourfile.xml").
Search for static files on Apache HTTP Server HowTo. Setup Apache to serve your xml files, make sure that you can view xml file in browser and then use the same url in mxUtils.load call.
I would like to find out if a filename exists in my Image folder, how can I do this in a function using javascript?
How about call a function from Javascript to a C# File? (ON SERVER)
Thank you
JavaScript from the browser does not have access to your Image folder on your local computer.
You have to use a server side language. Javascript won't have access to your folders on the server.
you have to provide more context
if your javascript is running within browser and you want to check if file exists on client's computer - you can't
if you want to check Image folder on server - you could do AJAX request for that image and then check if HTTP response code is 200 although that would also blow up your traffic and is not a great choice
best option would be to do AJAX request that will invoke a check on server side like /check?image_name=file.jpg
JavaScript is executed by Browser as Client-side scripting so it doesn't have access to local file system to check whether file exists or not to check file exists or not you need to use Server Side Scripting.