简体   繁体   中英

JavaScript to read local file

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM