【发布时间】:2012-03-09 17:39:08
【问题描述】:
试图弄清楚如何使用 PhoneGap 在文件系统上创建目录。
我想为我的 PhoneGap 应用程序创建一个目录,这样我就可以存储用户在那里创建的图像并将它们重新加载到应用程序中。
【问题讨论】:
标签: mobile cordova filesystems directory
试图弄清楚如何使用 PhoneGap 在文件系统上创建目录。
我想为我的 PhoneGap 应用程序创建一个目录,这样我就可以存储用户在那里创建的图像并将它们重新加载到应用程序中。
【问题讨论】:
标签: mobile cordova filesystems directory
这就是你的做法:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onRequestFileSystemSuccess, null);
function onRequestFileSystemSuccess(fileSystem) {
var entry=fileSystem.root;
entry.getDirectory("example", {create: true, exclusive: false}, onGetDirectorySuccess, onGetDirectoryFail);
}
function onGetDirectorySuccess(dir) {
console.log("Created dir "+dir.name);
}
function onGetDirectoryFail(error) {
console.log("Error creating directory "+error.code);
}
在 iOS 上,此脚本将在 Applications/YOUR_APP/Documents/ 中创建一个名为“example”的目录
【讨论】:
<preference name="AndroidPersistentFileLocation" value="Compatibility" />、<gap:plugin name="org.apache.cordova.file"/> 和 <gap:plugin name="org.apache.cordova.file-transfer"/>,但它仍然无法正常工作:(
此代码适用于我(基于版本 7 Cordova 文档 - https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html):
window.resolveLocalFileSystemURL('cdvfile://localhost/persistent/',
function fileEntryCallback(fileEntry) {
fileEntry.getDirectory('my_directory', { create: true });
}
);
如果上面的代码不起作用,您可能需要重新安装文件和文件传输插件。像
console.log(window.resolveLocalFileSystemUrl)这样的命令是测试插件是否正常工作的一种方法。
此外,如果您使用的是内容安全策略 (CSP),请确保您的 default-src 允许 cdvfile: 路径:
default-src cdvfile:
【讨论】: