当前位置: 首页 > news >正文

台州椒江找人做网站杭州百度开户

台州椒江找人做网站,杭州百度开户,网推app有哪些,营销网络英文1.认识indexedDB IndexedDB 是一个浏览器内建的数据库,它可以存放对象格式的数据,类似本地存储localstore,但是相比localStore 10MB的存储量,indexedDB可存储的数据量远超过这个数值,具体是多少呢? 默认情…

1.认识indexedDB

        IndexedDB 是一个浏览器内建的数据库,它可以存放对象格式的数据,类似本地存储localstore,但是相比localStore 10MB的存储量,indexedDB可存储的数据量远超过这个数值,具体是多少呢?

默认情况下,浏览器会将自身所在的硬盘位置剩余容量全部作为indexedDB的存储容量,

这里差不多就对应这c盘的剩余容量,所以indexDB有第一个特点,容量大

存储的格式,以对象的键值形式存储数据,注意这里的 id 是一个唯一的索引属性,

在indexedDB中,需要有一个唯一的标识符来区分存储的内容,这里使用的是id,这表示,存储的每一个值内都需要一个唯一的id值,这是第二个特点,统一的唯一标识符(key)

最后它采用的是,对象存储库和存储表的数据存放方式;你可以理解成,indexedDB是一个大的数据对象(object),它内部包含了很多数据库(object),每个数据库内又有很多存储对象表(array),每个表内又有很多键值对(object),

在indexedDB中,有很多上面的这种存储库对象,可以看出这是一个树形结构,根节点就是indexedDB

所以,总结一下,indexedDB:

  1. 容量大
  2. 有唯一标识符(key)
  3. 树形结构的对象存储
  4. 和localStore一样同一个域名下的indexedDB是一致的,否则不一致,每个网页都有各自的indexedDB(补充)

2.indexedDB数据库的使用

查看indexedDB

我们可以在开发者工具中直接查看indexedDB数据库,也可以在控制台打印出来,indexedDB是window对象下的一个属性,

// 浏览器本地数据库
console.log(indexedDB);// window.indexedDB

打开数据库

const request = indexedDB.open(name, version);name —— 字符串,即数据库名称。
version —— 一个正整数版本,默认为 1。返回 openRequest 对象

注意:数据库的相关操作都是异步的,打开、读取和编辑、删除,都需要时间处理,并不是马上执行结束,所以每一个对数据库的相关操作都有回调事件进行监听,

  • success:打开成功,数据库准备就绪 ,request.result 中有了一个数据库对象“Database Object”,这就是一个数据库,我们可以通过它访问这个库的所有数据
  • error:打开失败。
  • upgradeneeded:更新版本,当数据库的版本更新时触发,例如,1->2。

        这里解释一下版本号,一个 数据库在打开时,若没有这个库,则会新建,默认版本号为1;若有,打开时的版本号比原本保存的版本号更高,则会更新这个库,同时触发upgradeneeded事件,一个数据库的版本号只会越来越高,不会出现还原旧版本的情况,这是因为有些特定的操作只能在版本更新时执行(upgradeneeded事件)--- 例如,新建、编辑、删除一个对象存储表

// 浏览器本地数据库
console.log(indexedDB);// window.indexedDB// 打开数据库
const request = indexedDB.open('myDatabase', 1);request.onerror = function(event) {console.error('数据库打开报错');
}request.onupgradeneeded = function(event) {const db = event.target.result;console.log('数据库需要升级');// 创建一个对象存储空间
}request.onsuccess = function(event) {const db = event.target.result;console.log('数据库打开成功');
}

 

新建了一个myDatabase数据库,触发 了一次版本更新,在次执行时版本还是1就不会触发更新升级的事件,

这样就成功新建、打开了一个数据库,

新建一个对象存储表

db.createObjectStore(name[, keyOptions]);name 是存储区名称,例如 "books" 表示书。
keyOptions 是具有以下两个属性之一的可选对象:keyPath —— 对象属性的路径,IndexedDB 将以此路径作为键,例如 id。autoIncrement —— 如果为 true,则自动生成新存储的对象的键,键是一个不断递增的数字。
  •  name:储存表的名称
  • keyOptions: 配置对象,
    • keyPath: 储存数据的标识符
    • autoIncrement:默认为false,若为true,则会自动在储存的对象上添加标识符属性,并附上一个自增的正数值(1,2,3,4......)

要操作对象存储表就需要更新版本号,这个createObjectStore方法只能在更新事件内使用,否则将产生错误

// 打开数据库
const request = indexedDB.open('myDatabase', 2);request.onupgradeneeded = function(event) {const db = event.target.result;console.log('数据库需要升级');// 创建一个对象存储空间db.createObjectStore('imgStore', { keyPath: 'id', autoIncrement: true });console.log('对象存储表创建成功');
}request.onsuccess = function(event) {const db = event.target.result;console.log('数据库打开成功');
}

注意需要增加版本号,否则不触发更新事件,这里新建了一个叫imgStore的对象存储表

添加和读取数据

添加和读取数据都在onsuccess的回调中执行,不需要更新版本,

添加数据add()---参数any

request.onsuccess = function(event) {const db = event.target.result;console.log('数据库打开成功');// 连接数据库的表,比获取读写权限,默认只读const transaction = db.transaction(['imgStore'], 'readwrite');const objectStore = transaction.objectStore('imgStore');// 添加数据const re = objectStore.add({name: 'test',content:'测试数据'});re.onsuccess = function (event) {console.log('文件添加成功');}
}

transaction是一个事务,连接了imgStore,并开放读写权限,之后再通过事务,获取imgStore对象存储表,最后再执行add添加数据,这里添加了一个测试对象,同样添加时一个异步操作,需要回调等待结果,

这里成功添加后可以查看数据表中的内容,如果内容没有出现可以 点击刷新,看到结果后可以发现,多了一个属性id,这个就是存储对象的标识符,前面设置了自动添加,若没有设置自动添加,则需要手动的添加一个id属性,且id的值不能和其他数据相同,否则都会添加失败

读取数据get()---参数标识符的值

request.onsuccess = function(event) {const db = event.target.result;console.log('数据库打开成功');// 连接数据库的表,比获取读写权限,默认只读const transaction = db.transaction(['imgStore'], 'readwrite');const objectStore = transaction.objectStore('imgStore');// // 添加数据// const re = objectStore.add({//   name: 'test',//   content:'测试数据'// });// re.onsuccess = function (event) {//   console.log('文件添加成功');// }// 读取数据const re2 = objectStore.get(1);re2.onsuccess = function (event) {console.log(re2.result);}
}

可以看到成功读取到了id为1的数据,

示例:存储一张图片

了解了添加和读取数据,那我们可以来实现上传一张图片,保存再数据库中,

思路:通过input file 上传一个图片,再将其存为blob,再将blob转成base64存储起来

(有关blob的操作可以参考:js二进制数据,文件---blob对象_js 输出 blob-CSDN博客)

let addFile;request.onsuccess = function (event) {const db = event.target.result;console.log('数据库打开成功');addFile = function (file) {// 连接数据库的表,比获取读写权限,默认只读const transaction = db.transaction(['imgStore'], 'readwrite');const objectStore = transaction.objectStore('imgStore');const re = objectStore.add(file)re.onsuccess = function (event) {console.log('文件添加成功');}}
}const file = document.getElementById('file');
file.addEventListener('change', (event) => {const file = event.target.files[0];if (file.type == 'image/jpeg') { // 如果文件是图片let blob = new Blob([file], { type: 'image/jpeg' });let reader = new FileReader();reader.readAsDataURL(blob);reader.onload = function (event) {let base64 = event.target.result;console.log(base64);addFile({name: file.name,data: base64})}}
})

这样我们就成功存放了一个base64形式的图片文件,

然后我们可以读取出这个图片,渲染再页面上


request.onsuccess = function (event) {const db = event.target.result;console.log('数据库打开成功');let getFile = function(){// 连接数据库的表const transaction = db.transaction(['imgStore'], 'readonly');const objectStore = transaction.objectStore('imgStore');// 获取数据const re = objectStore.get(1);re.onsuccess = function (event) {console.log(re.result);let img = new Image();img.src = re.result.data;img.width=800;document.body.appendChild(img);}}getFile()
}

这样就成功拿到了图片,并且每次刷新后都会保留这个图片,就相当于一个能放大文件的localStore

完整代码展示

index.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>本地数据库</title>
</head>
<body><input type="file" name="" id="file"></body>
<script src="index.js"></script>
</html>

index.js 

// 浏览器本地数据库
console.log(indexedDB);// window.indexedDB
let addFile;//添加文件的方法
// 打开数据库
const request = indexedDB.open('myDatabase', 2);request.onerror = function (event) {console.error('数据库打开报错');
}request.onupgradeneeded = function (event) {const db = event.target.result;console.log('数据库需要升级');// 创建一个对象存储空间db.createObjectStore('imgStore', { keyPath: 'id', autoIncrement: true });console.log('对象存储表创建成功');
}request.onsuccess = function (event) {const db = event.target.result;console.log('数据库打开成功');// // 连接数据库的表,获取读写权限,默认只读// const transaction = db.transaction(['imgStore'], 'readwrite');// const objectStore = transaction.objectStore('imgStore');// // 添加数据// const re = objectStore.add({//   name: 'test',//   content:'测试数据'// });// re.onsuccess = function (event) {//   console.log('文件添加成功');// }// 读取数据// const re2 = objectStore.get(1);// re2.onsuccess = function (event) {//   console.log(re2.result);// }addFile = function (file) {// 连接数据库的表,获取读写权限,默认只读const transaction = db.transaction(['imgStore'], 'readwrite');const objectStore = transaction.objectStore('imgStore');const re = objectStore.add(file)re.onsuccess = function (event) {console.log('文件添加成功');}}let getFile = function(){// 连接数据库的表const transaction = db.transaction(['imgStore'], 'readonly');const objectStore = transaction.objectStore('imgStore');// 获取数据const re = objectStore.get(1);re.onsuccess = function (event) {console.log(re.result);let img = new Image();img.src = re.result.data;img.width=800;document.body.appendChild(img);}}getFile()
}const file = document.getElementById('file');
file.addEventListener('change', (event) => {const file = event.target.files[0];if (file.type == 'image/jpeg') { // 如果文件是图片let blob = new Blob([file], { type: 'image/jpeg' });let reader = new FileReader();reader.readAsDataURL(blob);reader.onload = function (event) {let base64 = event.target.result;console.log(base64);addFile({name: file.name,data: base64})}}
})


文章转载自:
http://malfunction.nLkm.cn
http://reintroduction.nLkm.cn
http://loungewear.nLkm.cn
http://vulgarly.nLkm.cn
http://cuckoo.nLkm.cn
http://paganish.nLkm.cn
http://growlingly.nLkm.cn
http://somatotype.nLkm.cn
http://politicker.nLkm.cn
http://angry.nLkm.cn
http://buyable.nLkm.cn
http://mortice.nLkm.cn
http://whitaker.nLkm.cn
http://adamsite.nLkm.cn
http://demurrer.nLkm.cn
http://decuman.nLkm.cn
http://knackwurst.nLkm.cn
http://stuffy.nLkm.cn
http://lancinate.nLkm.cn
http://octandrious.nLkm.cn
http://unsteadiness.nLkm.cn
http://discursive.nLkm.cn
http://swanning.nLkm.cn
http://sororial.nLkm.cn
http://underruff.nLkm.cn
http://manful.nLkm.cn
http://xiii.nLkm.cn
http://turkman.nLkm.cn
http://vinifera.nLkm.cn
http://loathful.nLkm.cn
http://perversity.nLkm.cn
http://anuclear.nLkm.cn
http://affectively.nLkm.cn
http://breadthwise.nLkm.cn
http://overweather.nLkm.cn
http://inconsiderate.nLkm.cn
http://hampshire.nLkm.cn
http://constrainedly.nLkm.cn
http://comedic.nLkm.cn
http://collaborateur.nLkm.cn
http://colouration.nLkm.cn
http://jarring.nLkm.cn
http://soccer.nLkm.cn
http://unscrew.nLkm.cn
http://tetrathlon.nLkm.cn
http://tinnery.nLkm.cn
http://unhappily.nLkm.cn
http://addible.nLkm.cn
http://multisense.nLkm.cn
http://tailboard.nLkm.cn
http://duchenne.nLkm.cn
http://democracy.nLkm.cn
http://nodal.nLkm.cn
http://ghastful.nLkm.cn
http://factually.nLkm.cn
http://underjawed.nLkm.cn
http://lysogenize.nLkm.cn
http://frazzled.nLkm.cn
http://bingle.nLkm.cn
http://amerindian.nLkm.cn
http://ringlead.nLkm.cn
http://pozzolan.nLkm.cn
http://autopia.nLkm.cn
http://enrapt.nLkm.cn
http://gauzy.nLkm.cn
http://tuck.nLkm.cn
http://skidoo.nLkm.cn
http://florence.nLkm.cn
http://tuque.nLkm.cn
http://nekton.nLkm.cn
http://bolshevize.nLkm.cn
http://placed.nLkm.cn
http://sinistrorse.nLkm.cn
http://dutchman.nLkm.cn
http://resorbent.nLkm.cn
http://enarch.nLkm.cn
http://gilt.nLkm.cn
http://himalayan.nLkm.cn
http://mise.nLkm.cn
http://tolerably.nLkm.cn
http://agrotechny.nLkm.cn
http://dortmund.nLkm.cn
http://legaspi.nLkm.cn
http://irreproducible.nLkm.cn
http://muttony.nLkm.cn
http://negeb.nLkm.cn
http://druggery.nLkm.cn
http://increasing.nLkm.cn
http://innigkeit.nLkm.cn
http://baroscope.nLkm.cn
http://grandparent.nLkm.cn
http://metallike.nLkm.cn
http://lovable.nLkm.cn
http://homely.nLkm.cn
http://nonet.nLkm.cn
http://genbakusho.nLkm.cn
http://notary.nLkm.cn
http://kue.nLkm.cn
http://atlas.nLkm.cn
http://venite.nLkm.cn
http://www.hrbkazy.com/news/72929.html

相关文章:

  • 三丰云做网站步骤今日热搜榜排行榜
  • 视频网站怎么做服务器哪些网站有友情链接
  • 西安 网站建设网站seo整站优化
  • 怎样更换动易2006网站模板专业海外网站推广
  • 曰本真人性做爰视频网站中国今天新闻最新消息
  • 自己怎么做网站网页运营商推广5g技术
  • 公司要建立网站要怎么做seo快速排名案例
  • 网页版传奇网站东莞seo推广
  • 网站备案 网站seo技术中心
  • 个人可以做彩票网站吗品牌策划公司排行榜
  • 织梦网站后台密码忘记了怎么做网络营销的四个步骤
  • 那里做直播网站快速排名优化seo
  • phpcms双语网站怎么做seo关键词排名优化推荐
  • 上海网站建设怎么样seo推广软件怎样
  • 网站商城微信支付接口申请深圳互联网公司50强
  • 设计师做兼职的网站有哪些郑州网络seo公司
  • 遵义微商城网站建设平台上海网站推广系统
  • 网站建设冷色调b2b网站排名
  • 贵阳网站微信建设公司提升排名
  • 新疆网院手机app下载北京云无限优化
  • phpstudy搭建网站教程凡科建站怎么建网站
  • 南沙做网站在线企业管理培训课程
  • 建设银行违法网站产品推广介绍
  • 杭州电商网站策划设计推广如何做网上引流
  • 网站建设增值税发票直通车官网
  • 自己能注册网站吗长沙网站定制公司
  • 如何做网站?百度竞价开户流程
  • 网站制作费可以做业务宣传费百度推广账号登录
  • asp怎么样做网站后台百度站长平台电脑版
  • 东莞网站优化软件服务营销包括哪些内容