IndexedDB demo showcase
var dbGlobals = new Object();
dbGlobals.db = null;
dbGlobals.description = "This database is used to store files locally.";
dbGlobals.name = "localFileStorage";
dbGlobals.version = 1;
dbGlobals.storeName = "fileObjects";
dbGlobals.message = "";
dbGlobals.empty = true; // --------------------------------------------------- function requiredFeaturesSupported() {
switch(window.location.protocol) {
case "http:":
break;
case "https:":
break;
case "ms-wwa-web":
break;
case "ms-wwa":
break;
default:
document.getElementById("bodyElement").innerHTML = "<h3>IndexedDB pages must be served via the http:// or https:// protocol - resolve this issue and try again.</h3>";
return false;
} // switch if(!document.getElementById("fileSelector").files) {
document.getElementById("bodyElement").innerHTML = "<h3>File API is not fully supported - upgrade your browser to the latest version.</h3>";
return false;
} if(!window.indexedDB) {
if(window.mozIndexedDB) {
window.indexedDB = widnow.mozIndexedDB;
}
else if(window.webkitIndexedDB) {
window.indexedDB = window.webkitIndexedDB;
IDBCursor = window.webkitIDBCursor;
IDBDatabaseException = window.webkitIDBDatabaseException;
IDBRequest = window.webkitIDBRequest;
IDBKeyRange = window.webkitIDBKeyRange;
IDBTransaction = window.webkitIDBTransaction;
}
else {
document.getElementById("bodyElement").innerHTML = "<h3>IndexedDB is not supported - upgrade your browser to the latest version.</h3>";
return false;
}
} // if if(!window.indexedDB.deleteDatabase) {
document.getElementById("bodyElement").innerHTML = "<h3>The required version of IndexedDB is not supported.</h3>";
return false;
}
return true;
} // requiredFeaturesSupported // -------------------------------------------------- if(requiredFeaturesSupported()) {
document.getElementById("openButton").addEventListener("click", openDB, false);
document.getElementById("populateButton").addEventListener("click", populateDB, false);
document.getElementById("displayButton").addEventListener("click", displayDB, false);
document.getElementById("deleteButton").addEventListener("click", deleteDB, false); document.getElementById("fileSelector").addEventListener("change", handleFileSelection, false);
} // if // ----------------------------------------- function openDB() {
console.log("------------------------openDB_onupgradeneeded()-----------------------");
displayMessage("<p>The database will be created/opened here...</p>"); if(!window.indexedDB.open) {
console.log("window.indexedDB.open is null in openDB()");
return;
} // if try {
var openRequest = window.indexedDB.open(dbGlobals.name, dbGlobals.version); // openRequest.onerror = function(evt) {console.log("openRequest.onerror fired in openDB() - error: " + (evt.target.error ? evt.target.error : evt.target.errorCode));};
openRequest.onblocked = openDB_onblocked;
openRequest.onupgradeneeded = openDB_onupgradeneeded;
openRequest.onsuccess = openDB_onsuccess;
}catch(ex) {
console.log("window.indexedDB.open exception in openDB() - " + ex.message);
}
} // openDB // -------------------------------------------------------------- function openDB_onblocked(evt) {
console.log("openDB_onblocked()"); var message = "<p>The database is blocked - error code: " + (evt.target.error ? evt.target.error : evt.target.errorCode) + "</p>";
message += "<p>If this page is open in other browser windows, close these windows.</p>"; displayMessage(message);
} // openDB_onblocked // -------------------------------------------- function openDB_onupgradeneeded(evt) {
console.log("openDB_onupgradeneeded()");
displayMessage("<p>Your request has been queued.</p>"); var db = dbGlobals.db = evt.currentTarget.result; // A successfully opened database results in a database object, which we place in our global IndexedDB variable. if(!db) {
console.log("db (i.e., evt.target.result) is null in openDB_onupgradeneeded()");
return;
} // if try {
db.createObjectStore(dbGlobals.storeName, {keyPath: "name"});
console.log("openDB_onupgradedneeded() success");
}
catch(ex) {
console.log("Exception is openDB_onupgradeneeded() - " + ex.message);
return;
} dbGlobals.message = "<p>The database has been created.</p>"; // A means of communicating this information to the openDB_onsuccess handler.
} // openDB_onupgradeneeded // ------------------------------------------------- function openDB_onsuccess(evt) {
console.log("openDB_onsuccess()");
displayMessage("<p>Your request has been queued.</p>"); var db = dbGlobals.db = evt.target.result; if(!db) {
console.log("db (i.e., evt.target.result) is null in openDB_onsuccess()");
return;
} // if dbGlobals.message += "<p>The database has been opened.</p>";
displayMessage(dbGlobals.message);
dbGlobals.message = "";
} // openDB_onsuccess // ---------------------------------------------- function populateDB() {
console.log("------------------------populateDB()--------------------------"); if(!dbGlobals.db) {
displayMessage("<p>The database hasn't been opened/created yet.</p>");
console.log("db (i.e., dbGlobals.db) is null in populateDB()");
return;
} document.getElementById("fileSelector").style.display = "block"; // Now that we have a valid database, allow the user to put file(s) in it. var message = "<p>Using the below <strong>Browse</strong> button, select one or more files to store in the database.</p>";
message += "<p>Then, click the <strong>Display DB<strong> button to display what's currently in the database.</p>";
displayMessage(message);
} // populateDB // ------------------------------------------------- function displayDB() {
console.log("------------------------displayDB()----------------------------"); var db = dbGlobals.db; if(!db) {
displayMessage("<p>There's no database to display.</p>");
console.log("db (i.e, dbGlobals.db) is null in displayDB()");
return;
} // if try{
var transaction = db.transaction(dbGlobals.storeName, (IDBTransaction.READ_ONLY ? IDBTransaction.READ_ONLY : "readonly"));
} // try
catch(ex) {
console.log("db.transaction() exception in displayDB() - " + ex.messsage);
return;
} // catch try{
var objectStore = transaction.objectStore(dbGlobals.storeName); try {
var cursorRequest = objectStore.openCursor(); cursorRequest.onerror = function(evt) {
console.log("cursorRequest.onerror fired in displayDB() - error code: " + (evt.target.error ? evt.target.error : evt.target.errorCode));
} var fileListHTML = "<p><strong>File(s) in database: </strong></p><ul style='margin: -0.5em 0 1em -1em;'>"; // Be aware that if the database is empty, this variable never gets used. cursorRequest.onsuccess = function(evt) {
console.log("cursorRequest.onsuccess fired in displayDB()"); var cursor = evt.target.result; if(cursor) {
dbGlobals.empty = false;
fileListHTML += "<li>" + cursor.value.name;
fileListHTML += "<p style='margin: 0 0 0 0.75em;'>" + cursor.value.name + "</p>";
fileListHTML += "<p style='margin: 0 0 0 0.75em;'>" + cursor.value.size + " bytes</p>";
cursor.continue();
}
else {
fileListHTML += "</ul>";
displayMessage(fileListHTML);
} if(dbGlobals.empty) {
displayMessage("<p>The database is empty – there's nothing to display.</p>");
}
}
} // inner try
catch(innerException) {
console.log("Inner try exception in displayDB() - " + innerException.message);
} // inner catch
} // outer try
catch(outerException) {
console.log("Outer try exception in displayDB() - " + outerException.message);
} // outer catch
} // displayDB // ------------------------------------------------- function deleteDB() {
console.log("------------------------deleteDB()-----------------------------");
displayMessage("<p>The database will be deleted here...</p>"); try{
if(dbGlobals.db) {
dbGlobals.db.close(); // If the database is open, you must first close the database connection before deleting it. Otherwise, the delete request waits (possibly forever) for the required close request to occur.
} var deleteRequest = window.indexedDB.deleteDatabase(dbGlobals.name); // Note that we already checked for the availability of the deleteDatabase() method in the above feature detection code.
deleteRequest.onsuccess = function() {
dbGlobals.db = null;
dbGlobals.empty = true;
dbGlobals.message = "";
displayMessage("<p>The database has been deleted.</p>");
console.log("delete success");
}; // deleteRequest.onsuccess
} // try
catch(ex) {
console.log("Exception in deleteDB() - " + ex.message);
} // catch
} // deleteDB // ------------------------------------------------- function handleFileSelection(evt) {
console.log("------------------------handleFileSelection()------------------------"); var files = evt.target.files; // The files selected by the uer (as a FileList object).
console.log(files);
if(!files) {
displayMessage("<p>At least one selected file is invalid - do not select any folders.</p><p>Please reselect and try again.</p>");
return;
} var db = dbGlobals.db;
if(!db) {
console.log("db (i.e., dbGlobals.db) is null in handleFileSelection()");
return;
} // if try{
var transaction = db.transaction(dbGlobals.storeName, (IDBTransaction.READ_WRITE ? IDBTransaction.READ_WRITE : "readwrite"));
} // try
catch(ex) {
console.log("db.transaction exception in handleFileTransaction() - " + ex.message);
return;
} // catch transaction.onerror = function(evt) {
console.log("transaction.onerror fired in handleFileSelection() - error code: " + (evt.target.error ? evt.target.error : evt.target.errorCode));
};
transaction.onabort = function() {
console.log("transaction.onabort fired in handleFileSelection()");
};
transaction.oncomplete = function() {
console.log("transaction.oncomplete fired in handleFileSelection()");
}; files = [
{
name: "sina.jpg",
size: 2813,
type: "text/html"
},
{
name: "indexedDB.html",
size: 808,
type: "text/html"
},
{
name: "m.html",
size: 0,
type: "text/html"
}
]; try {
var objectStore = transaction.objectStore(dbGlobals.storeName); for(var i = 0, file; file = files[i]; i++) {
var putRequest = objectStore.put(file);
putRequest.onsuccess = function() {dbGlobals.empty = false;};
putRequest.onerror = function(evt) {console.log("putRequest.onerror fired in handleFileSelection() - error code: " + (evt.target.error ? evt.target.error : evt.target.errorCode));};
} // for
} // try
catch(ex) {
console.log("Transaction and/or put() exception in handleFileSelection() - " + ex.message);
return;
} // catch document.getElementById("fileSelector").style.display = "none"; // The file(s) have already been selected so remove the "file picker" dialog box.
} // handleFileSelection // -------------------------------------------------- function displayMessage(message) {
document.getElementById("messages").innerHTML = message;
} // displayMessage // ------------------------------------------------------
http://www.huxiu.com/article/21008/1.html
IndexedDB demo showcase的更多相关文章
- Django Suit v2-dev 使用
转:链接:https://www.jianshu.com/p/84fa8219fb48 官方文档: 链接 Git: 链接 install Django Suit 为了适配 Django 有许多不同的版 ...
- 010. windows10下安装kivy 1.9.1版
Microsoft Windows [版本 10.0.14393] 以管理员权限打开cmd (c) 2016 Microsoft Corporation. 保留所有权利. 1. C:\Users\LG ...
- Kivy A to Z -- Kivy 示例演示自带名单
所有的样品已经在Android 4.04 手机正常进行 1.demo/kivycatalog 这个例子说明了如何使用主控件,例如Layout,Button,MediaPlayer,Progress B ...
- 95+强悍的jQuery图形效果插件
现在的网站越来越离不开图形,好的图像效果能让你的网站增色不少.通过JQuery图形效果插件可以很容易的给你的网站添加一些很酷的效果. 使用JQuery插件其实比想象的要容易很多,效果也超乎想象.在本文 ...
- indexedDB bootstrap angularjs 前端 MVC Demo
前端之MVC应用 1.indexedDB(Model): 数据层,前端浏览器 HTML5 API 面向对象数据库,一般现在用的数据库都是关系型数据库. 那么indexeddb有什么特点呢: 首先,从字 ...
- 通讯框架 T-io 学习——给初学者的Demo:ShowCase设计分析
前言 最近闲暇时间研究Springboot,正好需要用到即时通讯部分了,虽然springboot 有websocket,但是我还是看中了 t-io框架.看了部分源代码和示例,先把helloworld敲 ...
- js IndexedDB:浏览器端数据库的demo实例
IndexedDB具有以下特点. (1)键值对储存. IndexedDB内部采用对象仓库(object store)存放数据.所有类型的数据都可以直接存入,包括JavaScript对象.在对象仓库中, ...
- Web数据持久化存储IndexedDB(不常用)
IndexedDB是在浏览器中保存结构化数据的一种数据库,为了替换WebSQL(标准已废弃,但被广泛支持)而出现.IndexedDB使用NoSQL的形式来操作数据库,保存和读取是JavaScript对 ...
- HTML5 indexedDB数据库的入门学习(一)
笔者早些时间看过web sql database,但是不再维护和支持,所以最近初步学习了一下indexedDB数据库,首先indexedDB(简称IDB)和web sql database有很大的差别 ...
随机推荐
- c# 格式化百分比
代码示例: <%#Eval("total").ToString() == "0" ? "00.00%" : (Convert.ToDe ...
- Html+Css+Js_之table每隔3行显示不同的两种颜色
<html> <head> <script type="text/javascript"> /** 最近因项目的需求,有这样的一个问题: 一个t ...
- 使用DataList 分页方法
什么是DataList我想应该不需要解释了,接下来分享本人在项目里使用到的通过DataList进行分页展示方法. 首先在ASPX页面添加一个DataList(后面都简称DL)控件,示例代码如下: &l ...
- Apache虚拟目录
Apache虚拟目录 1.打开Apache的配置文件httpd.conf,并去掉#Include conf/extra/httpd-vhosts.conf前面的#! 2.在httpd.conf 末尾 ...
- iOS军火库-好用的ActionSheetView
GitHub地址 一个自定义的ActionSheetView,支持显示标题,默认选中,使用block回调. 使用说明 [GLActionSheet showWithDataSource:@[@&quo ...
- ios专题 - CocoaPods - 安装
职业走得很累,停下来,温故技术.顺便开始我得ios博客文章. [原创]http://www.cnblogs.com/luoguoqiang1985 安装 第一步:执行以下命令 sudo gem ins ...
- LCS最长公共子序列HDU1159
最近一直在学习算法,基本上都是在学习动态规划以及字符串.当然,两者交集最经典之一则是LCS问题. 首先LCS的问题基本上就是在字符串a,b之间找到最长的公共子序列,比如 YAOLONGBLOG 和 Y ...
- 3D Game Programming with directx 11 习题答案 8.2
第八章 第二题 1.首先找到Directx Texture Tool,它位于 2.填入配置 3.用画图工具画好每个level的图片,例如level0 4.用Directx Texture Tool添加 ...
- php 解决大流量网站访问量问题
当一个网站发展为知名网站的时候(如新浪,腾讯,网易,雅虎),网站的访问量通常都会非常大,如果使用虚拟主机的话,网站就会因为访问量过大而引起 服务器性能问题,这是很多人的烦恼,有人使用取消RSS等错误的 ...
- Nginx 独立图片服务器的搭建
为什么需要独立图片服务器? 如果你留心的话,可以发现,现在主流的网站都是有单独的图片服务器的,例如,人人网的为rrimg,淘宝的为taobaocdn,下面还有很多的二级域名. 独立的图片服务器有诸多好 ...