How to access filename from a folder using JavaScript - javascript

I am trying to build a simple website with just an index file and a folder full of icons for our internal purpose. I am making this so that anyone in our team can just access the link and download the icons whenever needed without our help. I am trying to read the icons file name from the folder automatically, so every time we update the folder with new ones we don't have to edit the HTML. I know only JavaScript. Please help me.

An alternative could be to produce a filelist of the files via a script or batch file (depending on OS, something like - in psudeo - ls path/to/images > filelist.txt) which redirect the output to a file that ends up in the same root as the index and images.
This file could then be read via fetch() or XMLHttpRequest() and parsed on client side to provide the basis for the links to the updates files.
This would require an extra step in the pipe-line of course, but could easily be automated using a listener for the image directory that triggers the script, part of your project setup and so forth.

This is not possible. In your case Javascript is running in the browser (client-side). It has no access to the servers filesystem. You need a server-side dynamic web-service (php, node.js, ...)

Related

Js - Create a file and save it to a specific folder in the project

I created an excel file via javascript, but when I need to save it the only option is to have the user open the download window and download the file. I, on the other hand, would like the file to simply be stored in a folder in my project, without the user having the option to download the file.
How can I do this? Thank you
You don't provide enough information but seems that you are doing it on the client side (i.e. a browser), which, in principle, does not have access to the file system on your computer/server.
You should try some Back-end approach, like, in your case with JavaScript: Node.js, or maybe anothers like Python or PHP.

Can a Chrome Extension Dynamically Add JS Files to Local Extension Directory? User Upload or Saved From the Web?

I'm wondering if it's possible to for certain JS files to be added to the web extension directory later?
Like say I have an app where users can select certain settings from within the app and those files (js and html files, images or blobs) are somehow added into the extension from the web. Like some sort of ondemand updater without using any native apps but it seems that upgrades are done by the appstores automatically.
I'm reading the files using ajax and adding them to indexeddb but because it could be more than one file that's getting messy.
Say a user wants a certain feature on the extension and there's an html page, js files and images then this gets downloaded to a certain folder inside the installed extension.
function download() { //only saves to downloads directory
var imgurl = "https://www.google.com.hk/images/srpr/logo11w.png";
console.log('download');
browser.downloads.download({url:imgurl},function(downloadId){
console.log("download begin, the downId is:" + downloadId);
});
}
I also tried the chrome download function above but that only works for the downloads folder not the extension folder.
Is there any way to make a custom updater?! I know we can't save to disk but any leniency or workarounds for the extension folder?! Even something silly like making a shell call to some dos (and linux/mac) thing that saves the file to the extension folder. I can fetch the files, just not save them.
Ok so I'll put it as an answer. This is the solution I'm leaning on which works for my scenario and I've listed some alternatives below:
Having the other files as separate extensions and giving the user an install link instead where they can install that extension, then those child extensions talk to the mother extension and they know the address to the resources in their child extension folder, so the mother gets the just the file locations from the children to load those assets from that folder. The child extensions are like bundles of those html and js with a background script which sends the addresses of these items to the mother.
https://developer.chrome.com/extensions/messaging#external
The drawback is that I'll have to see how that affects the urls like if I inject the html page from the child extension folder into the main interface using ajax then I can't use relative url's to any images in that 'cos the urls are relative to the mother extension folder.. I'll have to rewrite the child extension urls with the absolute paths into the html page to load images and js from the child extension html code which has relative urls.
Pros:
Cleaner and more persistent than indexeddb.
Files can be loaded normally from disk.
Cons:
User has to install separate extensions.
URL structure might be a bit confusing, need to rewrite urls if loading html from child. However this is only for image src's and where the javascript is loaded from so it's not such a big deal.
Other Possible Solutions:
Indexeddb which I'm already doing seems to be the preferred way of doing this but I really do not want to store every html asset in indexeddb. The upside is that while extensions need to be installed, this method can be done silently fetching and adding files without user interaction and indexeddb seems to be somewhat persistent. Might still end up using this because it is silent but having to load each asset from a database sounds like a nightmare.
The File Handle Api might have worked if I was working on Firefox only https://wiki.mozilla.org/WebAPI/FileHandleAPI
I haven't tried the shell copy, maybe if I fetch with ajax and then save to disk using some dos function and then doing different save functions for different OS systems.
Filesystem Api only saves to downloads and doesn't work for extensions anyways, so that's useless.
UPDATE
In windows there isn't any sudo, but this worked without admin priveleges for a subfolder (not on the C:\ root though). It would work for a linux only app very nicely. If I just wanted to save a file to a windows machine this might work.
Shell copy method would be to grab the contents of file with ajax from the local or remote location, output to DOS as a stream to save to file on windows. And do this for every operating system with a shell exec command or detect the OS and do that command. This way I can even put the files in the exact folder location.
Like say I make this sort of command from the contents:
//To append you can use >> instead of >
//folder seems necessary, can't save to root without admin
echo the content I want to save > C:\folder\textfile.txt
I thought of calling it using shell exec that only works in nodejs, so digging through the other answers on
How to execute shell command in Javascript
//full code to save file using javascript on windows
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("echo content to save > C:\folder\textfile.txt");
The shell command doesn't seem to work. i can't find what this is for. There doesn't seem to be a shell command in regular javascript for windows. It seems to require IE ActiveX. Doesn't work with Firefox or Chrome.
Extensions can't modify their sources because the browser verifies them and resets/disables the extension if they change. Also, in Firefox the extensions aren't even unpacked.
The solution is actually quite trivial: save the code in any storage (localStorage, chrome.storage.local, IndexedDB) as a string and then add it in your extension page as a standard DOM script element. You'll have to relax the standard CSP a bit for that.

Build URL to data file download with link to folder

I have a web application that triggers actions at the server side which result in the generation of data files.
The so generated data files are stored within a folder at drive different from the one where the Web application is.
What I tried so far is to create a shortcut (Windows Server) next to the Index.html, where the shortcut points to the folder at the second drive.
This solution does not appear to work:
If I copy the data file to the same folder where the Index.html file is and enter navigate to the URL httm://127.0.0.1:324324/Data_File.xlsx I get the file downloaded.
If I enter httm://127.0.0.1:324324/MyShortcut/Data_File.xlsx (where MyShortcut points to a folder in drive D) the attempt fails.
How can I achieve this?
The need to access files in the different drive is essential.
You need to configure your server to serve that folder under different location. The only reason you are able to see your app and access that folder is cause server is set up to serve it ATM, not because you are making the right calls from the front or cause the front is asking nicely.
Don't know what Windows server version you are using and IIS version that goes on it, or are you even using IIS (you most probably do) but depending on what you use you need to do something in a lines of this:
IIS7 config
What you probably wanna do is create virtual folder in IIS. Try looking into it, then if you fail ask a question with proper tags according where are you stuck.
I don't see how this is connected to front-end at all so front end tags wont get you far.
EDIT:
Oh, in that case get a second file server running for that folder? This should be the fastest way, use what you can from this list: list

Access folders and files for creating a html page

I am new to HTML. I need to create a webpage that reads system path from user and display the statistics about folders and files in a table format. And i want to do the following things
1) Read for folders under path
2) To access sub folders under folder
3) Read the files under a path
4) To get the path of files (to create hyperlink)
For Example, I need to go to the system path given by user in a webpage and read folders in it. For each folders i have to read subfolders and files in it and create a table that displays the name of subfolders under the parent folder and other information about files
So in addition to html what should i learn to do these things. Thanks in advance.
First of all, HTML is used to define and format the presentation of text, images, and other web content, only. And therefore, in order to handle user input and execute server side commands (like listing directory/sub-directories content), you need to write server side code.
Server side languages allow you to create dynamic web content; in your case, code that would:
process the user input,
execute server side command to list system files,
and generate the tables and their content from the return result of the executed command.
php is straightforward easy-to-begin-with language. You can find a basic introduction to php on W3Schools. PHP 5 Tutorial - W3Schools
If you choose to go for php, check the following php function scandir. This php built-in function allow you to list files and directories inside a specified path.
Nodewebkit or Electron both can give you ability to write HTML/CSS/Javascript that will be executed in client machine with access to users filesystem. Check them and see if they fit your scenario.
In short, you will have to compile your HTML/CSS/Javascript as a standalone executable file (various Operating systems are supported) and provide that executable to the client so he/she can run it.

phonegap update file from external server

hi
i have a fully working phonegap application with a number of files (both .csv and .jpg) that i would like to check for updates on a web server (say example.com/app) when certain pages are loaded. if there are new files i would like to overwrite the files on the phone app with these updated files
for example:
when you click to:
page2.html, check for updates on 2.csv and 2.jpg
Thanks in advice
Tim
You should look into client side HTML5 storage.
I'm using the HTML5 sqllite database to store json objects relating to each page within my app. I then have a master configuration file which is in essence a sitemap with each app page, and a timestamp, when I check against the webserver configuration file copy I run a comparison to see if any files are out of date/even new and if so download and store within the client database.
Also take a look at
https://github.com/brianleroux/lawnchair
http://sixrevisions.com/web-development/html5-iphone-app/
Hope this helps
regards
Andrew
On the HTML page, the same originator rule will restrict you from reading the files in javascript.
I guess you have to create a method in the phonegap wrapper which will check for the files. This method can be called from within javascript.

Categories