-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.html
More file actions
71 lines (61 loc) · 2.26 KB
/
Copy pathimport.html
File metadata and controls
71 lines (61 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import black</title>
<style>
body{
background-color: black;
color: white;
}
</style>
</head>
<body>
<h1>Import data file</h1>
<input type="file" id="fileInput" accept=".json" />
<button onclick="importData()">Import Data</button>
<script>
function importData() {
const file = document.getElementById('fileInput').files[0];
if (!file) {
alert('Please select a JSON file.');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
try {
const data = JSON.parse(e.target.result);
// alert(JSON.stringify(data));
if (typeof data !== 'object' || Array.isArray(data)) throw new Error();
const dbName = 'black';
const storeName = 'sst';
const dbReq = indexedDB.open(dbName,2);
dbReq.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(storeName)) {
db.createObjectStore(storeName);
}
};
dbReq.onsuccess = (event) => {
const db = event.target.result;
const tx = db.transaction(storeName, 'readwrite');
const os = tx.objectStore(storeName);
for (const key in data) {
console.log("nkn chakli");
os.put(data[key],key);
}
tx.oncomplete = () => {
alert('Data imported successfully!');
db.close();
};
tx.onerror = () => alert('Error import.');
};
dbReq.onerror = () => alert('Error database.');
} catch (err) {
alert('Invalid JSON file');
}
};
reader.readAsText(file);
}
</script>
</body>
</html>