gpt4 book ai didi

javascript - 文件系统 API - 从本地驱动器上传到本地文件系统

转载 作者:搜寻专家 更新时间:2023-10-31 23:10:06 25 4
gpt4 key购买 nike

我已经阅读了很多关于文件系统 API 和 HTML5 的内容,但我找不到有效的解决方案,所以我问你们:

我想要一个文件上传表单,拖放或常规输入框都没有关系,但是我想选择一个文件,上传后应该将文件或整个文件夹“上传”到位于的文件系统在客户端计算机上。上传在括号中,因为我实际上想将文件/文件夹复制到客户端本地文件系统。

有可能吗?因为我想制作一个应用程序,用户可以在其中将他的文件(例如音乐或大型视频和电影)上传到他的本地文件系统并在我的应用程序中编辑/观看等。我知道我必须上传那些大文件,我必须将它们切成碎片并将它们堆叠起来,但我只想从小做起 :)

提前致谢

最佳答案

目前关于这个主题的信息确实很少,所以我整理了一个结合的例子:

  • 使用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);
});

关于javascript - 文件系统 API - 从本地驱动器上传到本地文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10720704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com