Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  194] [ 4]  / answers: 1 / hits: 37799  / 7 Years ago, wed, february 15, 2017, 12:00:00

How do I upload the content of a folder using JavaScript (client side)? The FileSystem API has not been adopted by browsers other than Chrome; I only get a File item with the name of the folder.



It should be possible, because Google Drive allows to drop a folder and all the content (folders and files) will be uploaded automatically.


More From » javascript

 Answers
7

It seems that Chrome and Firefox supports part of the filesystem API, but are not oficially supported.


This allows you to drop a folder and read all the content, here's the code i use on my app.


    function addFiles(e){
e.stopPropagation();
e.preventDefault();

// if directory support is available
if(e.dataTransfer && e.dataTransfer.items)
{
var items = e.dataTransfer.items;
for (var i=0; i<items.length; i++) {
var item = items[i].webkitGetAsEntry();

if (item) {
addDirectory(item);
}
}
return;
}

// Fallback
var files = e.target.files || e.dataTransfer.files;
if (!files.length)
{
alert('File type not accepted');
return;
}

processFile(files);
}

function addDirectory(item) {
var _this = this;
if (item.isDirectory) {
var directoryReader = item.createReader();
directoryReader.readEntries(function(entries) {
entries.forEach(function(entry) {
_this.addDirectory(entry);
});
});
} else {
item.file(function(file){
processFile([file],0);
});
}
},

[#58936] Sunday, February 12, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arlethjeanettep

Total Points: 506
Total Questions: 96
Total Answers: 79

Location: Liberia
Member since Tue, Mar 14, 2023
1 Year ago
;