在 Chrome 应用程序中删除文件后访问 fileEntry
Access fileEntry after dropping a file in a Chrome App
是否可以通过拖放打开文件在 Chrome 应用程序中获取 fileEntry
对象?当我将文件放入我的应用程序时,我只得到一个似乎与文件系统无关的 file
对象。更改后无法使用该对象保存文件。
我得到这样的文件:
document.body.addEventListener('drop', function (event) {
file = event.dataTransfer.files[0]
});
我想做什么
我正在开发一个文本编辑器,我想添加一个通过将文件拖到我的应用程序中来打开文件的功能。
正如我所说:我已经获得了文件的内容,但我无法将更改写回文件,因为我需要一个 fileEntry
对象才能这样做。
好的,我只是在检查 event
对象时发现的。在事件对象中有一个名为 webkitGetAsEntry()
的函数来获取 fileEntry
对象。这是正确的代码:
document.body.addEventListener('drop', function (event) {
fileEntry = event.dataTransfer.items[0].webkitGetAsEntry();
});
这是您可以用来将更改写回文件系统的对象。
参考代码:
// Of course this needs the "fileSystem" permission.
// Dropped files from the file system aren't writable by default.
// So we need to make it writable first.
chrome.fileSystem.getWritableEntry(fileEntry, function (writableEntry) {
writableEntry.createWriter(function (writer) {
// Here use `writer.write(blob)` to write changes to the file system.
// Very important detail when you write files:
// https://developer.chrome.com/apps/app_codelab_filesystem
// look for the part that reads `if (!truncated)`
// This also is very hard to find and causes an annoying error
// when you don't know how to correctly truncate files
// while writing content to the files...
});
});
是否可以通过拖放打开文件在 Chrome 应用程序中获取 fileEntry
对象?当我将文件放入我的应用程序时,我只得到一个似乎与文件系统无关的 file
对象。更改后无法使用该对象保存文件。
我得到这样的文件:
document.body.addEventListener('drop', function (event) {
file = event.dataTransfer.files[0]
});
我想做什么
我正在开发一个文本编辑器,我想添加一个通过将文件拖到我的应用程序中来打开文件的功能。
正如我所说:我已经获得了文件的内容,但我无法将更改写回文件,因为我需要一个 fileEntry
对象才能这样做。
好的,我只是在检查 event
对象时发现的。在事件对象中有一个名为 webkitGetAsEntry()
的函数来获取 fileEntry
对象。这是正确的代码:
document.body.addEventListener('drop', function (event) {
fileEntry = event.dataTransfer.items[0].webkitGetAsEntry();
});
这是您可以用来将更改写回文件系统的对象。
参考代码:
// Of course this needs the "fileSystem" permission.
// Dropped files from the file system aren't writable by default.
// So we need to make it writable first.
chrome.fileSystem.getWritableEntry(fileEntry, function (writableEntry) {
writableEntry.createWriter(function (writer) {
// Here use `writer.write(blob)` to write changes to the file system.
// Very important detail when you write files:
// https://developer.chrome.com/apps/app_codelab_filesystem
// look for the part that reads `if (!truncated)`
// This also is very hard to find and causes an annoying error
// when you don't know how to correctly truncate files
// while writing content to the files...
});
});