javascript - 在 Chrome 应用程序中删除文件后访问 fileEntry

标签 javascript google-chrome drag-and-drop google-chrome-app file-access

是否可以通过拖放打开文件在 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...
    });
});

关于javascript - 在 Chrome 应用程序中删除文件后访问 fileEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338494/

相关文章:

google-chrome - Chrome 中打包应用程序与旧版打包应用程序之间的区别

java - 执行 selenium 测试时,浏览器未在 Jenkins 中打开

javascript - JavaScript 中的鼠标事件问题

javascript - 垂直点导航中事件点的颜色不会改变

php - 如何构建 jQuery 删除

javascript - 这不会经常发生 : Why is this JavaScript code only broken in Chrome?

objective-c - 在 NSTableView 中实现拖放

cocoa - 放置在 NSTableView 上的行为

javascript - 在运行时在 Highcharts 的图表中添加按钮

javascript - 重新定义 'expect'