如何创建一个新的文件 [英] How to create a new file

查看:189
本文介绍了如何创建一个新的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PhoneGap的 - 如何创建一个新的文件资产/网络/例子/ hallo.text
我的错误是文件系统错误

  window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,失败);功能失效(){
  确认('错误的文件系统');
}功能gotFS(文件系统){
  fileSystem.root.getFile(文件:///android_asset/www/example/hallo.text,{
    创建:真
  },gotFileEntry,失败);
}功能gotFileEntry(FileEntry的){
  fileEntry.createWriter(gotFileWriter,失败);
}功能gotFileWriter(作家){
  writer.write(你好);
}


解决方案

HTML文件系统API在沙盒文件系统中运行。不能引用绝对路径相对完整的文件系统;相反,所有的Web应用程序的文件系统存在于设备的真正firesystem内浏览器生成的子目录。

这是故意做的,出于安全原因,使Web应用程序只能覆盖或阅读他们已经创建的文件。否则,在网络上的任何页面可能会读你的整个硬盘驱动器的全部私人内容。

您应该使用像 WWW /例子/ hallo.text (假设这些目录存在),或者干脆 hallo.text ,以便在沙盒文件系统中创建文件。不要使用绝对文件:路径

此外,你的误差函数,失败,可能正在用错误的信息参数提供。尝试添加一个参数传递给函数如函数失败(错误)... ,并打印出错误更多调试信息

PhoneGap - How to create a new file in assets/www/example/hallo.text my error is "Error fileSystem"

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function fail() {
  confirm('Error  fileSystem');
}

function gotFS(fileSystem) {
  fileSystem.root.getFile("file:///android_asset/www/example/hallo.text", {
    create: true
  }, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
  fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
  writer.write("hallo");
}

解决方案

The HTML file system API operates in a sandboxed file system. You cannot reference absolute paths with respect to the full filesystem; instead, all of your Web app's file system exists in a browser-generated subdirectory inside the device's real firesystem.

This was done deliberately, for security reasons, so that Web apps can only overwrite or read files that they have created. Otherwise, any page on the Web might read all the private contents of your whole hard drive.

You should use an unqualified file path like www/example/hallo.text (assuming those directories exist) or simply hallo.text to create the file in your sandboxed file system. Do not use an absolute file: path.

Additionally, your error function, fail, is probably being supplied with an argument with error info. Try adding an argument to the function like function fail(error) ... and printing out error for more debugging info.

这篇关于如何创建一个新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆