I am novice in javascript. Is it possible to search in a directory of html files for a text content or string?
If possible can anyone suggest me how to implement this or any reference.
JavaScript has no native ability access files or directories. You need an API provided by the host environment to do that.
NodeJS, for example, provides the File System module.
JavaScript embedded in a webpage has no access to the client's file system (for security reasons). It can access URLs (via XMLHttpRequest) which a web server can map onto a file system.
Well yes it is possible. But modern browsers will restrict access to the client's disk so no you will not be allowed to read the disk without serious changes in browser specific security settings.
Related
I am wondering if there is any method of reading file on the client side with javascript given the file name and path. I am aware of the method of reading through: input type="file".
What I mean is without opening up a file browser and selecting the file, just reading it with filename, like fopen.
If not an other method, how to instantiate a File object in javascript from filename and path?
Seeing the answers I thought of briefing a bit more on my purpose.
I am not really making a webpage. I am using browser just as a software that is available on any system. The html pages will all be local. I am doing this way be so that I have the advantage of the power of js. Given this situation is there any way to set permission flags that will let it read files?
Thank you.
There's the Drag 'n Drop API which would be the other option in reading files using JS
There will always be hurdles so that your code will not directly read from the client's filesystem. This is designed for privacy and security purposes.
Extensions/Plugins - sandboxing will be your enemy, limiting file system access
Java Applet - Needs a signed certificate to read the filesystem. A self-signed certificate will generate the "Will You Trust This Applet" prompt which would act as the security prompt.
Input type=file - gives the user the option to select the files or not
drag and drop - gives the user the option to drag in files or not
XHR - Same Origin Policy will block you, especially if you are executing the page locally and not on a server.
JSONP - Since this bypasses the SOP, however, your file should be in JSONP format.
I found solution to my problem, here it is...
It is true that we have restriction with javascript that whenever the user or the page requests for a file it has to go through that extra window that pops up,
but if all the files that you may request are inside a known folder then you can rather ask the user to point the zipped folder instead.
One can use tools like zip.js for opening a file that one needs in the zipped folder.
Thank you :)
I am writing a very simple Elearning application for my virtual class. I wander if there may be a way using Javascript that works on the background to read the content of a file located on the user's computer and send its content to my server. Any one knows an open source code of Javascript doing this ?
I think that this can be done using XMLHttpRequest, i.e. AJAX, but I could not graps my way through. Any help
You can't gain access to a local file in browser because you are in sandbox, but there are some File API for Html 5 that you can programmatically select them and access their data just have a look at the following link
File API
Web applications should have the ability to manipulate as wide as possible a range of user input, including files that a user may wish to upload to a remote server or manipulate inside a rich web application. This specification defines the basic representations for files, lists of files, errors raised by access to files, and programmatic ways to read files. Additionally, this specification also defines an interface that represents "raw data" which can be asynchronously processed on the main thread of conforming user agents. The interfaces and API defined in this specification can be used with other interfaces and APIs exposed to the web platform.
Unfortunately there is no way to give JavaScript any access to the user's local files.
There is a very serious security issue with that.
There is however API for that on IE browsers (Which give nice big warning when you attempt to access the files)
I recommend to ask from the user to upload his own file (using <input type="file" />) and upload it to the server.
HTML5 specifications specify an API to access the local filesystem. It is supported in the latest versions of Chrome. See here for details http://net.tutsplus.com/tutorials/html-css-techniques/toying-with-the-html5-filesystem-api/.
If the environment is controlled (i.e in a classroom or intranet) you can build your application using this API.
It seems that this has been asked a number of times, but I would like to make sure there are still no other alternatives.
So what I need to do is:
Generate a .JSON file from .net (easy)
Read the local JSON file with JavaScript and produce an html page with useful graphs etc (using Raphaeljs).
Deliver the outcome (HTML + associated JavaScript) to the client.
I wouldn't mind installing a local Apache to read the JSON file easily but since I cannot ask the client to do this, I am looking for a way to read a local JSON file (on the filesystem, no web server) with JavaScript, so that I eventually provide the client with the HTML, .js and .json and he can see the associated graphs.
I 've been reading that FileReader class is an option but I want to have the flexibility to support these on a number of IE browsers (let say IE7 to IE10).
Any help on how to work round this would be much appreciated.
Web browsers are not able to read files from the file system due to security reasons. If I've understood correctly, you have a .NET application running on the local machine?
If this is the case, you can host a WCF service to serve the JSON from your application. WCF self-hosting is described on this MSDN page. You should be able to make AJAX requests from the browser to a server hosted within you application on localhost.
If you want to support older IE, you need to go into ActiveX Security setting hell. Look into var fso = new ActiveXObject("Scripting.FileSystemObject");
But if you are delivering the content and HTML files to the client, why don't you just add the JSON markup directly into the html document? Seems like the EASY solution.
You can't read file from local disk in browser (security restriction). But once you are going to deliver HTML + js and JSON (in zip?) you can read json like from regular web server. Just use relative links and if JSON will be in same folder where HTML and JS is located. Or you may put your JSON as a part of HTML you deliver to customer.
But if you are going to show a page (HTML and JS) on your site in web, you should better ask client to upload their JSON file, and than download it from server (in AJAX style).
How to access path of a folder instead of a file using Ext.form.field in extjs. Is it possible at all in extjs.
I can use the component above but its giving me only the file name without the path to the file.
Or should I use third party tools like jquery or maybe dynamic html/ plain js?
There's no way to get any information on the client's filesystem using a website, for security reasons. Nor Javascript, nor PHP...
Thus, you can't get the path of a client's file.
However, you can do it with a non-website application, installed on his computer.
You could use a java, flash, or activex control to do that.
You could also host extjs inside Sencha Desktop Packaging, ie in an installed executable with an embedded browser.
You might be able to get folder access with HTML5, but that would only be a folder local to your domain and heavily restricted.
Otherwise, browser file/folder security restrictions exist for good reason and you should consider files off limits other than upload/download.
I want to list the names of HTML files in a particular folder using JavaScript (in the browser)...
Can anybody help me in this?
Thank You
If you're using Javascript that's running in the browser, there's no way to "open a folder". You have to obtain data about the folder contents through some data structure or by doing something like parsing a server generated folder index in HTML.
If you're using Javascript that's running on a non-browser engine (such as jscript, rhino, etc.) then you have to be more specific in the question as the answer will obviously depend on whether the engine where your script is running provides objects to access the filesystem or not.
This is what you are asking for:
http://www.irt.org/articles/js014/index.htm
Also read on FSO
But yes, if you can't use FSO if ActiveX is disabled on client system.
Javascript can't interact with the OS while running inside a browser, due to security concerns.
It can get a list from the server and display that to the client on the browser, but, to get a directory structure from the client computer to the browser would require some other application reading the directory structure and sending it to the server, and the server sending it to the browser.
Now, if you are using Actionscript it is possible to read the hard drive, and I expect that Silverlight would allow you to do this also, but I don't know.