IndexedDB over local HTML file

 ̄綄美尐妖づ 提交于 2019-12-11 02:37:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!