JavaScript to read local file - javascript

Is there any way to enable native JavaScript to read local file on the disk?
no Ajax, no JQuery or other JavaScript library, only use the native JavaScript?
I have searched, but it seems there is no way to do this.
My requirements are that when the Internet can not be accessed, I will show a warning page in the Chrome browser.

HTML5, have a File Api, if you are interested in reading file, just for the reading - MSDN, HTML5rocks.
If you are more interested in the case when the user is offline, and your application needs a storage, you can take a look at localstorage - MDN, diveintohtml5. There is an example how to check if it is available, although CanIUse says it is currently available everywhere. Here is a live example.
Hope this will help, in your case which seems to be the synchronization after being offline.

If you are using JavaScript in a browser, there is no way to access an arbitrary file from the local filesystem like this. XMLHTTPRequest can be used to access files natively without a library like jQuery, but it prevents access to local files as a security measure.
However, you can use a cache.manifest file to set up an offline version of an HTML 5 site/app.

You can use a combination of <input type="file"/> and Javascript's window.FileReader to read file. It has many methods of receiving the data, easiest being FileReader.readAsBinaryString(file); You can only read files that a user has selected from a file browser.
Here's Mozilla Developer Network's documentation on it.
For your uses, however, it seems like developing a Chrome extension would be a better solution to manipulating the way Google Chrome works.

Related

Does pdf.js execute javascript contained in a pdf file?

It appears that PDF has, for lack of a better term the "feature" to contain javascript, but luckily it seems there is a change that only some software implements support for javascript
Since web browsers deal already with the issue to handle tons of untrusted input, and hence do some sort of sandboxing, I rather appreciate to open pdf documents inside the browser sandbox via pdf.js. However I wonder if pdf.js will implement the "javascript contained in pdf file misfeature"?
pdf.js still does not support embedded javascript
There are some other implementations (e.g. https://www.pdftron.com/webviewer/demo/pdf-forms) but most of them have some limitations unless you are looking for simple field/form calculations
Update 2022: Since Summer 2021, pdf.js (and by extension, my library ngx-extended-pdf-viewer) uses Quick.js to run JavaScript in a sandbox. You've already mentioned that the browser runs JavaScript in a sandbox, so this is a sandbox running in a sandbox. The Mozilla team didn't want to give the JavaScript code embedded in the PDF file access to browser resources.
If you're using Firefox as a PDF viewer, you're using a slightly different version of pdf.js. It runs the JavaScript code in the same sandbox that runs Firefox WebExtensions.
Here's an article written by the authors of the JavaScript implementation: https://hacks.mozilla.org/2021/10/implementing-form-filling-and-accessibility-in-the-firefox-pdf-viewer/

Can I use Javascript to save a file to the user's file system in Firefox and Safari?

I understand that this is possible in Chrome and Opera, using the HTML5 FileWriter API, but this is not supported for Firefox and Safari. Is there any other solutions?
You didn't give enough details for a good answer ... things like
Is this a one-time download
Do you need to read-back this information?
I'm going to address the question, as you asked it:
Yes, there is a way to save a file to the local file system on many of the newer browsers, but it can't be down automatically by using the HTML5 download attribute.
The user-experience is much the same as downloading other types of files.
More details can be found here:
http://webreflection.blogspot.com/2011/08/html5-how-to-create-downloads-on-fly.html

How can I analyze a file about to be uploaded before it's actually uploaded?

We are currently planning a website on which people can upload movies. When looking at YouTube you notice that some movies are uploaded twice or more times (by different users). To scale our application we're thinking about the following idea:
User selects movie file to be uploaded
A JavaScript will get the SHA256 hash from the file (it's more accurate then the MD5 hash) before it get's uploaded
The website will check if the hash already exists
If the hash doesn't exist, the file will be uploaded
If the hash does exist a message will be prompted or a reference to the already existing version on the server will be created. This without the video being uploaded.
Q: How do we analyze a file with JavaScript in order to get the SHA256 hash, and is SHA256 good enough or should we consider SHA512 (or another algorithm)?
Use the HTML5 File API to read the file: http://www.html5rocks.com/en/tutorials/file/dndfiles. Here is a JS code for calculating SHA-256: http://www.webtoolkit.info/javascript-sha256.html
I must add that I never tried this, but it seems to be possible. Alxandr is right, this would take very long for large videos, but you may try to use the WebWorker API in order not to freeze the browser: http://html5rocks.com/en/tutorials/workers/basics
Putting files aside for now, if the question is actually whether it's possible to get a SHA-256 hashes in JavaScript, the answer is yes. You can either reiplement this yourself (bad idea) or use a library like the Stanford JS Crypto library (good idea).
As far as the File API goes, it is implemented in the bleeding edge version of every major desktop browser, as well as the latest Android browser shipping. iOS is not supported as it doesn't really have a filesystem. Check out caniuse.com for the latest stats.
Simple answer, you can't. That is if you want to support all browsers at least. I think both Chrome and FireFox supports the reading of files on the client, and IE supports it with the help of ActiveX controls, but to get a solution that works in all browsers you have to use a plugin like Flash or Silverlight. Also, when doing file-uploads of video-magnitude (large+ files), I think going for flash or the likes from the start is a good idea anyhow, but that's just my opinion.

What Local Databases are avaliable for Javascript?

I'm developing a project in Server Side Javascript (SJS) using POW webserver. The result of the project will be a local application for users that will interact with a sort of "Database" of Answers and Questions (It's like a Quiz Builder).
Actually we are just starting and we are storing informations into JSON files (POW allows read/write of plain files), but I'm looking for a more efficient way to store and recall information from the DB (something SLQ-like).
Is there any possibility of doing this using Javascript? One of the requirements is that the user doesn't have to install a DB server (Mysql, Sqlite ecc) so it should be a Javascript-pure way, or even XML-HTML5 way (I'm not going to use WebDB or similars).
There are two client-side database features in modern browsers: IndexedDB and Web SQL.
Neither is fully supported across the current versions of the various browsers, but IndexedDB seems to be the one which is going to win -- the Web SQL is apparently no longer being maintained, and may be dropped entirely.
You can find out more about them both, and their respective browser support on the CanIUse website.
[EDIT] As mentioned by #Raynos, there is also LocalStorage, which actually appears to have much better cross-browser support than either of the other two mentioned above. Given the browser support (even IE8 supports it!), this would seem to be the best option for the time being, even though it isn't as fully featured as the other options above.
Here's the CanIUse details for LocalStorage.
Hope that helps.
POW provides simple access to SQLite DB, saved in pow/data folder.
http://davidkellogg.com/wiki/Pow_db

Understanding browser plugins: creation, deployment, and use

Let's say I wanted to create a browser plug-in that would open someone's floppy drive for them whenever they click a button on my web page. (You remember that old gag? - Don't worry, I don't really want to do that.)
What are the steps necessary to create a browser plug-in that will work on most browsers and most operating systems?
How do I deploy the plug-in? For instance, how do I make it so that the plug-in can be easily downloaded and installed? Do I have to worry about digital certificates?
How do I check for and use the pluggin once it's installed? Is it available to javascript in the form of an API?
Take a look at a project called FireBreath: http://www.firebreath.org/display/documentation/FireBreath+Home
Firebreath abstracts many of the differences between the browser plugin frameworks and platform issues so you can easily write the code for a plugin once and just re-compile it for different platforms.
I have found it good for exposing existing C/C++ library API's to Javascript so I can write dynamic pages that use functionality from the C++ library which were otherwsie not available.
Most modern browsers have their own plugin frameworks for developers to utilize. So, for example, a Firefox plugin will not work in chrome or IE. Firefox and chrome do both utilize javascript and css but they are fundamentally different in their structure.
As far as deployment, Mozilla and Google provide hosting for their respective plugins. I do know that firefox extensions are basically zip files with the extension changed.
EDIT...
I had assumed you meant browser extensions so disregard my answer if that is not the case.
The easiest cross-browser solution is use Java Applets or maybe Flash (I am not sure how you do it in Flash).
With applets, you would need to sign the applet and also create a security policy file for accessing the disk. When the applet loads in the browser it would ask permission from the client user, he/she needs to click Allow to give permission for the applet to access the disk. This would allow you to access any disk in the system.

Categories

Resources