As web apps evolve, you might have found it handy to let users drag
and drop files from the desktop onto the browser to edit, upload, share,
etc. But unfortunately, we’ve been unable to drag and drop folders onto
web pages. Luckily, beginning with Chrome 21, this issue will be
addressed (already available in the Chrome dev channel).
Passing multiple files with drag and drop
Let’s look at a code sample of existing drag and drop.
<divid=”dropzone”></div>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e) {
var length = e.dataTransfer.files.length;
for (var i = 0; i < length; i++) {
var file = e.dataTransfer.files[i];
... // do whatever you want
}
};
In this example, you can actually drag and drop
a file or files from the desktop to your browser, but when you try to
pass a folder, notice that it will be either rejected or treated as a File object resulting in a failure.
How to handle dropped folders
Chrome 21 allows you to drop a folder or multiple folders into the
browser window. To handle these, you need to change the way you handle
dropped objects.
<divid=”dropzone”></div>
var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e) {
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++) {
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isFile) {
... // do whatever you want
} else if (entry.isDirectory) {
... // do whatever you want
}
}
};
Notice that a big difference here is that you can treat a dropped object as Entry (FileEntry or DirectoryEntry) by using new function called getAsEntry (webkitGetAsEntry).
After obtaining access to the Entry object, you can use
standard file handling methods that were introduced in the FileSystem
API specification. For example, this example shows how you can detect if
a dropped object is a file or a directory by examining the .isFile (or the .isDirectory) field.
For further information regarding the FileSystem API, read Exploring the FileSystem APIs, regarding new drag and drop capability, read this document. Of course, these features are open standards or are waiting to become one soon.
In a recent post, Eiji Kitamura highlighted a subtle, yet powerful new feature in the drag and drop APIs; the ability to drag in folders and retrieve them as HTML5 Filesystem API FileEntry and DirectoryEntry objects (done by accessing a new method on the DataTransferItem, .webkitGetAsEntry()).
What's remarkably cool about the .webkitGetAsEntry() extension is how elegant it makes importing files and entire folders. Once you have a FileEntry or DirectoryEntry from a drop event, it's a matter of using the Filesystem API's copyTo() to get it imported into your app.
An example of copying multiple dropped folders over to the filesystem:
var fs =null;// Cache filesystem for later.// Not shown: setup drag and drop event listeners.function onDrop(e){
e.preventDefault();
e.stopPropagation();var items = e.dataTransfer.items;for(var i =0, item; item = items[i];++i){var entry = item.webkitGetAsEntry();// Folder? Copy the DirectoryEntry over to our local filesystem.if(entry.isDirectory){
entry.copyTo(fs.root,null,function(copiedEntry){// ...}, onError);}}}
window.webkitRequestFileSystem(TEMPORARY,1024*1204,function(fileSystem){
fs = fileSystem;},function(e){
console.log('Error', e);});
Very nice! Again, the simplicity comes from integrating DnD with the Filesystem API calls.
Taking this one step further, we also have the ability to drag and drop a folder and/or files onto a normal <input type="file">, then access the entries as Filesystem directory or file entries. That is done through .webkitEntries:
<inputtype="file"multiple>
function onChange(e) {
e.stopPropagation();
e.preventDefault();
var entries = e.target.webkitEntries; // Get all dropped items as FS API entries.
[].forEach.call(entries, function(entry) {
// Copy the entry into our local filesystem.
entry.copyTo(fs.root, null, function(copiedEntry) {
...
}, onError);
});
}
document.querySelector('input[type="file"]').addEventListener('change', onChange);
I've put together a photo gallery demo to demonstrate these different techniques for importing files/folders.
그립다는 거, 그건 차라리 절실한 생존 같은 거 가을은 구름밭에 파란 우물을 판다 그리운 얼굴을 비치기 위하여
- 조병화의 시<가을>(전문)에서 -
* 여름이 물러가고 가을이 살랑살랑 다가오고 있습니다.
산자락 가을 하늘에도 푸른 우물이 번져갑니다.
나뭇잎 사이에도 군데군데 푸른 우물이 보입니다.
"아, 가을이구나! 하늘도 높고 정말 푸르구나!"
잠시 걸음을 멈춰 가을 하늘을 다시 봅니다.
어린 시절, 그리운 얼굴이 보입니다.
어느덧 내 눈도 스르르 젖어
우물이 됩니다.