如何使用 chrome 应用程序文件系统将 FileEntry 转换为标准 JavaScript 文件对象
How to convert FileEntry to standard JavaScript File object using chrome apps fileSystem
是否可以将 FileEntry
转换为标准 JavaScript 对象 File
?
我在文档中找不到任何有意义的内容 https://developer.chrome.com/apps/fileSystem
我在 google 示例中找到了如何执行此操作 https://github.com/GoogleChrome/chrome-app-samples/blob/master/samples/filesystem-access/js/app.js
var jsFileObject;
fileEntry.file(function (file){
jsFileObject = file
});
FileEntry
documentation 确实在此处提供指导:
The FileSystemFileEntry
interface of the File System API represents a
file in a file system. It offers properties describing the file's
attributes, as well as the file()
method, which creates a File object
that can be used to read the file.
不幸的是 file()
方法依赖于回调而不是 Promises,但我们可以包装它并使使用 API(和阅读代码)更容易:
async function getFile(fileEntry) {
try {
return await new Promise((resolve, reject) => fileEntry.file(resolve, reject));
} catch (err) {
console.log(err);
}
}
var file = await getFile(fileEntry);
请注意,FileSystemFileEntry
是非标准功能,不是标准轨道,因此 API 和浏览器支持可能会改变!
是否可以将 FileEntry
转换为标准 JavaScript 对象 File
?
我在文档中找不到任何有意义的内容 https://developer.chrome.com/apps/fileSystem
我在 google 示例中找到了如何执行此操作 https://github.com/GoogleChrome/chrome-app-samples/blob/master/samples/filesystem-access/js/app.js
var jsFileObject;
fileEntry.file(function (file){
jsFileObject = file
});
FileEntry
documentation 确实在此处提供指导:
The
FileSystemFileEntry
interface of the File System API represents a file in a file system. It offers properties describing the file's attributes, as well as thefile()
method, which creates a File object that can be used to read the file.
不幸的是 file()
方法依赖于回调而不是 Promises,但我们可以包装它并使使用 API(和阅读代码)更容易:
async function getFile(fileEntry) {
try {
return await new Promise((resolve, reject) => fileEntry.file(resolve, reject));
} catch (err) {
console.log(err);
}
}
var file = await getFile(fileEntry);
请注意,FileSystemFileEntry
是非标准功能,不是标准轨道,因此 API 和浏览器支持可能会改变!