问题
I would like to develop an application which uses a browser to interact with the user. The application is to be available offline and distributable via a zip.
The functions I would like to perform are to be handled by HTML, CSS, JS and I would like to make use of the IndexedDB functionality.
I have hit a problem with IndexedDB (Chrome) in that the same code works in an online space but not from a local hard drive location (file://).
Refer to example: http://jsfiddle.net/FwuUD/
(function() {
var db;
var dbreq = indexedDB.open("TestApp", 2);
dbreq.onsuccess = function(e) {
alert("Database created");
db = e.target.result;
var employeeStore = db.createObjectStore (
"employees",
{keyPath: "id"}
);
};
dbreq.onerror = function(e) {
alert("Database Error: " + e.target.errorCode);
};
dbreq.onupgradeneeded = function(e) {
alert("Database upgrade needed");
};
})();
Any suggestions?
回答1:
The indexedDB API only works inside a webserver. When you navigate to it using the file system it won't work. The indexedDB API needs a domain context to work in and the file system doesn't provide that. Short you need an url to use the api.
回答2:
IndexedDB is disabled when run from file:/// for security. It's unclear if you control the browser parameters. If you do, you can pass --allow-file-access-from-files which will allow IndexedDB to work from the file:/// origin.
回答3:
In case anyone is looking for an updated answer to this 6-year-old question, both Chrome and Safari allow local HTML files to access IndexedDB API now, without the need for a local server. IE continues to hold out on this it seems. Unsure about Edge or Firefox.
来源:https://stackoverflow.com/questions/15694334/indexeddb-over-local-html-file