現時点では、この件に関する情報はほとんどないため、組み合わせた例をまとめました。
- の
webkitdirectory
属性を使用します<input type="file">
。
- これにより、ユーザーは適切なダイアログ ボックスを使用してディレクトリを選択できます。
- ファイルシステム API の使用。
- これは、クライアントのマシンにファイルを保存できるサンドボックス化されたファイルシステムに関するものです。
- ファイル API の使用。
- これは、ファイルを読み取るための API です。
<input type="file">
ファイルには、エレメント、ドラッグ アンド ドロップを使用した転送、またはファイルシステム API を介してアクセスできます。
これらは現在 Chrome でのみ適切に機能webkit
するため、必要に応じてプレフィックスを使用しました。
http://jsfiddle.net/zLna6/3/
コード自体には、明確であることを願っているコメントがあります。
var fs,
err = function(e) {
throw e;
};
// request the sandboxed filesystem
webkitRequestFileSystem(
window.TEMPORARY,
5 * 1024 * 1024,
function(_fs) {
fs = _fs;
},
err
);
// when a directory is selected
$(":file").on("change", function() {
$("ul").empty();
// the selected files
var files = this.files;
if(!files) return;
// this function copies the file into the sandboxed filesystem
function save(i) {
var file = files[i];
var text = file ? file.name : "Done!";
// show the filename in the list
$("<li>").text(text).appendTo("ul");
if(!file) return;
// create a sandboxed file
fs.root.getFile(
file.name,
{ create: true },
function(fileEntry) {
// create a writer that can put data in the file
fileEntry.createWriter(function(writer) {
writer.onwriteend = function() {
// when done, continue to the next file
save(i + 1);
};
writer.onerror = err;
// this will read the contents of the current file
var fr = new FileReader;
fr.onloadend = function() {
// create a blob as that's what the
// file writer wants
var builder = new WebKitBlobBuilder;
builder.append(fr.result);
writer.write(builder.getBlob());
};
fr.onerror = err;
fr.readAsArrayBuffer(file);
}, err);
},
err
);
}
save(0);
});
$("ul").on("click", "li:not(:last)", function() {
// get the entry with this filename from the sandboxed filesystem
fs.root.getFile($(this).text(), {}, function(fileEntry) {
// get the file from the entry
fileEntry.file(function(file) {
// this will read the contents of the sandboxed file
var fr = new FileReader;
fr.onloadend = function() {
// log part of it
console.log(fr.result.slice(0, 100));
};
fr.readAsBinaryString(file);
});
}, err);
});