我希望在数据目录中包含一些文件作为我的应用程序的一部分。我正在使用 Visual Studio 创建应用程序。
如何包含要部署到数据目录的文件?我是否在我的项目中创建某个文件夹?我需要将它们标记为内容吗?
亲切的问候
我希望在数据目录中包含一些文件作为我的应用程序的一部分。我正在使用 Visual Studio 创建应用程序。
如何包含要部署到数据目录的文件?我是否在我的项目中创建某个文件夹?我需要将它们标记为内容吗?
亲切的问候
您可以编写一些代码,在运行时将所需文件从只读www/
目录复制到读写目录。我找到了一篇博客文章,准确地描述了这一点:
// copy a database file from www/ in the app directory to the data directory
function copyDatabaseFile(dbName) {
var sourceFileName = cordova.file.applicationDirectory + 'www/' + dbName;
var targetDirName = cordova.file.dataDirectory;
// resolve the source and target filenames simultaneously
return Promise.all([
new Promise(function (resolve, reject) {
resolveLocalFileSystemURL(sourceFileName, resolve, reject);
}),
new Promise(function (resolve, reject) {
resolveLocalFileSystemURL(targetDirName, resolve, reject);
})
]).then(function (files) {
var sourceFile = files[0];
var targetDir = files[1];
// try to fetch the target file, to check if it exists
return new Promise(function (resolve, reject) {
targetDir.getFile(dbName, {}, resolve, reject);
}).catch(function () {
// target file doesn't exist already, so copy it
return new Promise(function (resolve, reject) {
sourceFile.copyTo(targetDir, dbName, resolve, reject);
});
});
});
}
deviceready
在 cordova 发出事件后,您可以在您的应用程序入口点调用类似的函数。