有確實在眼下這個問題的資料很少,所以我放在一起,結合了一個例子:
- 上
<input type="file">
使用webkitdirectory
屬性。
- 使用Filesystem API。
- 這是關於沙盒文件系統,它允許您將文件存儲在客戶端的機器上。
- 使用File 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);
});
你的意思是「選擇文件(S)來自客戶機的文件系統,並寫(複製)他們到其他位置在客戶端文件系統中「?沒有「上傳」。 – Bergi