-
Notifications
You must be signed in to change notification settings - Fork 0
/
Javascript CRUD files.html
239 lines (207 loc) · 8.15 KB
/
Javascript CRUD files.html
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html>
<head>
<title>JavaScript CRUD files</title>
<link rel='stylesheet' type='text/css' href='./CSS/home.css'>
<style>
#drop_zone, #files {
height: 300px;
border-style: dashed;
margin: 20px;
}
#byte_content {
margin: 5px 0;
max-height: 100px;
overflow-y: auto;
overflow-x: hidden;
}
#byte_range { margin-top: 5px; }
#progress_bar {
margin: 10px 0;
padding: 3px;
border: 1px solid #000;
font-size: 14px;
clear: both;
opacity: 0;
-moz-transition: opacity 1s linear;
-o-transition: opacity 1s linear;
-webkit-transition: opacity 1s linear;
}
#progress_bar.loading {
opacity: 1.0;
}
#progress_bar .percent {
background-color: #99ccff;
height: auto;
width: 0;
}
</style>
</head>
<body>
<H1>JavaScript CRUD files</H1>
<div id='container'>
<h3> Demo </h3>
<p>Note: Some browsers treat input type='file' elements as native drop targets. </p>
<input type='file' id='files' name='files[]' multiple />
<div id='drop_zone'>Drop files here</div>
<button onclick='abortRead();'>Cancel read</button>
<div id='progress_bar'><div class='percent'>0%</div></div>
<output id='list'></output>
<span class='readBytesButtons'>
<button data-startbyte='0' data-endbyte='49'>1-50</button>
<button data-startbyte='50' data-endbyte='99'>51-100</button>
<button data-startbyte='100' data-endbyte='149'>101-150</button>
<button>entire file</button>
</span>
<div id='byte_range'></div>
<div id='byte_content'></div>
</div>
<div id='footer'>
<h3> Document Information </h3>
<ul>
<li>
Date: 2015/04/13
</li>
<li>
Reference:
<ul>
<li><a target='_blank' href='http://www.html5rocks.com/en/tutorials/file/dndfiles/'>Reading files in javascript using the File APIs</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
<script src='./LIB/jquery/jquery-2.1.3.js'></script>
<script src='./LIB/home.js'></script>
<script type='text/javascript'>
/*
Select Files
*/
(function(){
try{
if (window.File && window.FileReader && window.FileList && window.Blob){
console.log('The File APIs are fully supported in the browser.');
} else {
console.log('The File APIs are not fully supported in the browser.');
}
var reader;
var progress = document.querySelector('.percent');
function abortRead() {
reader.abort();
}
function errorHandler(evt) {
switch(evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
alert('File Not Found!');
break;
case evt.target.error.NOT_READABLE_ERR:
alert('File is not readable');
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
alert('An error occurred reading this file.');
};
}
function updateProgress(evt) {
// evt is an ProgressEvent.
if (evt.lengthComputable) {
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
if (percentLoaded < 100) {
progress.style.width = percentLoaded + '%';
progress.textContent = percentLoaded + '%';
}
}
}
function readBlob(opt_startByte, opt_stopByte) {
var files = document.getElementById('files').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
var start = parseInt(opt_startByte) || 0;
var stop = parseInt(opt_stopByte) || file.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
document.getElementById('byte_content').textContent = evt.target.result;
document.getElementById('byte_range').textContent =
['Read bytes: ', start + 1, ' - ', stop + 1,
' of ', file.size, ' byte file'].join('');
}
};
if (file.webkitSlice) {
var blob = file.webkitSlice(start, stop + 1);
} else if (file.mozSlice) {
var blob = file.mozSlice(start, stop + 1);
} else {
var blob = file.slice(start, stop + 1);
}
if(blob){
reader.readAsText(blob);
//reader.readAsBinaryString(blob);
}
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
// Reset progress indicator on new file selection.
progress.style.width = '0%';
progress.textContent = '0%';
var files;
if(!files){
files = evt.target.files; // FileList object
}
if(!files){
files = evt.dataTransfer.files;
}
// files is a FileList of File objects. List some properties.
var output = [];
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
reader.onerror = errorHandler;
reader.onprogress = updateProgress;
reader.onabort = function(e) {
console.log('File read cancelled');
};
reader.onloadstart = function(e) {
document.getElementById('progress_bar').className = 'loading';
};
reader.onload = function(e) {
// Ensure that the progress bar displays 100% at the end.
progress.style.width = '100%';
progress.textContent = '100%';
setTimeout('document.getElementById("progress_bar").className="";', 2000);
}
reader.readAsText(f);
//reader.readAsBinaryString(f);
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a', '</li>');
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
if (evt.target.tagName.toLowerCase() == 'button') {
var startByte = evt.target.getAttribute('data-startbyte');
var endByte = evt.target.getAttribute('data-endbyte');
readBlob(startByte, endByte);
}
}, false);
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
// Setup the dnd listeners.
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
}catch(e){
console.error(e.message);
}
}());
</script>