我在JavaScript和HTML5中创建应用程序。我需要将数据写入特定的文件,所以我想在代码中设置文件的路径。使用以下命令来实现这一点并不成问题:
function onInitFs(fs) {
console.log('Opened file system: ' + fs.name);
}
window.webkitRequestFileSystem(window.TEMPORARY, 5 * 1024 * 1024 /* 5MB */, onInitFs, errHandl);但问题是,它将调用一个安全错误,因为没有为exe文件设置--allow-file-access-from-files。我不想在所有页面上都设置得很难。我想只为我的页面设置该选项,即我从本地文件系统打开的页面
例如,它可以是index.html:
C:\page\index.html.有没有办法做到这一点?
我只知道manifest.json,但这意味着我必须通过Chrome商店分发我的应用程序。这对我来说是不可接受的,我的项目将不会有任何意义。
发布于 2013-10-15 20:05:54
FileAPI的思想是拥有一个虚拟的沙盒文件系统,而不是管理OS文件系统。你不能编辑用户驱动器中的任意文件,这将是一个巨大的安全漏洞。
你可能想做的,是动态生成页面的。如下例所示,首先保存内容(html),然后加载并添加到页面:
function onInitFs(fs) {
console.log('Opened file system: ' + fs.name);
fs.root.getFile(FILE_NAME, {create: true}, function(fileEntry){
fileEntry.createWriter(function(fw){
fw.onwriteend = function(e){ console.info('Write completed'); }
fw.onerror = function(e){ console.log('Error:'+e.toString()); }
var blobToWrite = new Blob(['<span>test</span>'], {type: 'text/plain'});
fw.write(blobToWrite);
}, null);
console.log(fileEntry.toURL());
}, null);
fs.root.getFile(FILE_NAME, {}, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(e) {
var someDiv = document.createElement('div');
someDiv.innerHTML = this.result;
document.body.appendChild(someDiv);
};
reader.readAsText(file);
}, null);
}, null);
}希望这能有所帮助:)
https://stackoverflow.com/questions/13168088
复制相似问题