给定文件的完整路径,如何使用新的 Cordova 文件插件?
Given the full path to a file, how you use the new Cordova file plugin?
Cordova 文件插件在此处有详细记录:http://ngcordova.com/docs/plugins/file/
然而,我遇到的问题是这些方法通常需要一个 FileSystem
对象和一个表示文件名的字符串。然而,我所拥有的是文件的完整路径,它可以来自任何可读 FileSystem
。事实上,文件路径是使用 Cordova 相机插件(http://ngcordova.com/docs/plugins/camera/)使用 destinationType Camera.DestinationType.FILE_URI.
检索的
话虽如此,我如何仅使用完全解析的文件路径调用 readAsBinaryString(FileSystem, fileName) 方法?
您可能正在寻找 window.resolveLocalFileSystemURL
。文件插件使用一些 html5 调用来完成它的工作。
这是我得到的文档导入方法的示例。 (它非常简单;如果你病态地好奇,完整的文件就在这里:https://github.com/adapt-it/adapt-it-mobile/blob/master/www/js/views/DocumentViews.js)
importFile = function (file, project) {
var reader = new FileReader();
reader.onloadend = function (e) {
// do your parsing here
};
reader.readAsText(file);
}
window.resolveLocalFileSystemURL(fileURL,
function (entry) {
entry.file(
function (file) {
importFile(file);
},
function (error) {
console.log("FileEntry.file error: " + error.code);
}
);
},
function (error) {
console.log("resolveLocalFileSystemURL error: " + error.code);
});
Raymon Camden 也有一个很棒的博客系列,解释了与 Cordova 相关的文件 API 的详细信息。这是关于读取文件的:http://www.raymondcamden.com/2014/07/15/Cordova-Sample-Reading-a-text-file.
Cordova 文件插件在此处有详细记录:http://ngcordova.com/docs/plugins/file/
然而,我遇到的问题是这些方法通常需要一个 FileSystem
对象和一个表示文件名的字符串。然而,我所拥有的是文件的完整路径,它可以来自任何可读 FileSystem
。事实上,文件路径是使用 Cordova 相机插件(http://ngcordova.com/docs/plugins/camera/)使用 destinationType Camera.DestinationType.FILE_URI.
话虽如此,我如何仅使用完全解析的文件路径调用 readAsBinaryString(FileSystem, fileName) 方法?
您可能正在寻找 window.resolveLocalFileSystemURL
。文件插件使用一些 html5 调用来完成它的工作。
这是我得到的文档导入方法的示例。 (它非常简单;如果你病态地好奇,完整的文件就在这里:https://github.com/adapt-it/adapt-it-mobile/blob/master/www/js/views/DocumentViews.js)
importFile = function (file, project) {
var reader = new FileReader();
reader.onloadend = function (e) {
// do your parsing here
};
reader.readAsText(file);
}
window.resolveLocalFileSystemURL(fileURL,
function (entry) {
entry.file(
function (file) {
importFile(file);
},
function (error) {
console.log("FileEntry.file error: " + error.code);
}
);
},
function (error) {
console.log("resolveLocalFileSystemURL error: " + error.code);
});
Raymon Camden 也有一个很棒的博客系列,解释了与 Cordova 相关的文件 API 的详细信息。这是关于读取文件的:http://www.raymondcamden.com/2014/07/15/Cordova-Sample-Reading-a-text-file.