Skip to content

Instantly share code, notes, and snippets.

@clemos
Created January 13, 2017 17:07
Show Gist options
  • Save clemos/7596ece535b85063b804a361a4aa03cd to your computer and use it in GitHub Desktop.
Save clemos/7596ece535b85063b804a361a4aa03cd to your computer and use it in GitHub Desktop.
test web file
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var opts = {
type: window.PERSISTENT,
size: 1024*1024
};
function FileView(ul) {
var files = [];
var doc = ul.ownerDocument;
function clear(el){
while(el.firstChild){
el.removeChild(el.firstChild);
}
}
function preview(file) {
var url = URL.createObjectURL(file);
var span = doc.createElement('span');
span.className = 'file-preview';
if( file.type.indexOf('image') > -1 ) {
var img = doc.createElement('img');
img.src = url;
span.appendChild(img);
} else if ( file.type.indexOf('video') > -1 ) {
var video = doc.createElement('video');
video.controls = true;
video.src = url;
span.appendChild(video);
} else {
span.contentText = 'no preview';
}
return span;
}
function draw(){
clear(ul);
for( var i=0, file; file = files[i]; i++ ) {
var li = doc.createElement('li');
li.className = 'file-entry';
li.appendChild( preview(file) );
li.innerHTML += ""
+ "<span class='file-relative-path'>" + file.webkitRelativePath + "</span>"
+ "<span class='file-name'>" + file.name + "</span>"
+ "<span class='file-size'>" + file.size + "</span>"
+ "<span class='file-type'>" + file.type + "</span>"
+ "<span class='file-last-modified'>" + file.lastModifiedDate + "</span>";
ul.appendChild(li);
};
}
this.setFiles = function(f){
files = f;
draw();
}
}
var fileInput = document.querySelector('#file-input');
var view = new FileView(document.querySelector("#view"));
function onError() {
console.error(arguments);
}
function onFileSystem(fs) {
console.log('got filesystem', fs);
}
window.requestFileSystem(opts.type, opts.size, onFileSystem, onError);
fileInput.onchange = function onFileInput(e) {
console.log('got files',this.files);
view.setFiles(this.files);
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<input id="file-input" type="file" webkitdirectory multiple>
<ul id="view">
</ul>
<script src="app.js"></script>
</body>
</html>
body, html {
font-family:sans-serif;
}
#view {
margin:0;
padding:0;
}
.file-preview {
display:inline-block;
width: 320px;
height: 240px;
overflow:hidden;
background:lightgrey;
text-align:center;
}
.file-preview img,
.file-preview video {
max-width: 100%;
max-height: 100%;
}
.file-entry {
list-style:none;
padding:0;
margin:0 0 4px 0;
}
.file-entry span {
margin-left:10px;
font-size:10px;
color:grey;
}
.file-entry .file-relative-path {
font-size:12px;
color:#111;
font-family:monospace;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment