【问题标题】:FileSystem on Cordova 3.4.0 fails "Could not create target file"Cordova 3.4.0 上的 FileSystem 失败“无法创建目标文件”
【发布时间】:2014-03-06 01:59:44
【问题描述】:

我最近将我的 iOS Cordova 项目从 2.7.0 升级到了 3.4.0。

升级文件系统后访问被破坏。 (但似乎可以在模拟器中工作?)

我收到一条错误消息,指出“无法创建目标文件”,我四处搜索并想将“完整路径”更改为“toURL()”,但无济于事。我真的不知道接下来该尝试什么?

这是我的下载代码

window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,

function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
    "dummy.html", {
    create: true,
    exclusive: false
},

function gotFileEntry(fileEntry) {
    var sPath = fileEntry.toURL().replace("dummy.html", "");
    var fileTransfer = new FileTransfer();
    fileEntry.remove();

    fileTransfer.download(
        "https://dl.dropbox.com/u/13253550/db02.xml",
    sPath + "database.xml",

    function (theFile) {
        console.log("download complete: " + theFile.toURI());
        showLink(theFile.toURI());
        setTimeout(function () {
            checkConnection();
        }, 50);
    },

    function (error) {
        console.log("download error source " + error.source);
        console.log("download error target " + error.target);
        console.log("upload error code: " + error.code);
    });
},
fail);
},
fail);

【问题讨论】:

  • 您是否也升级了文件插件? 2.7.0 到 3.4.0 是一个很大的飞跃。
  • 是的,所有插件都已更新到最新版本。虽然我同意这是一个很大的飞跃,但我之前在其他项目中也做过类似的飞跃,但这是第一个有问题的项目

标签: cordova phonegap-plugins cordova-plugins


【解决方案1】:

我找到了文件插件 ( link) 和文件传输插件 ( link) 的文档

在进行原始问题中提到的更改后,我想知道文件插件部分是否正常,并开始寻找我的 fileTransfer 代码与提供的示例之间的差异。

原来我没有在我的下载源 URL (doh) 上执行 encodeURI()

完整的工作代码:

window.requestFileSystem(
LocalFileSystem.PERSISTENT, 0,

function onFileSystemSuccess(fileSystem) {
fileSystem.root.getFile(
"dummy.html", {
create: true,
exclusive: false
},

function gotFileEntry(fileEntry) {
var sPath = fileEntry.toURL().replace("dummy.html", "");
var fileTransfer = new FileTransfer();
fileEntry.remove();
var DBuri = encodeURI("https://dl.dropbox.com/u/13253550/db02.xml");
fileTransfer.download(
    DBuri,
sPath + "database.xml",

function (theFile) {
    console.log("download complete: " + theFile.toURI());
    showLink(theFile.toURI());
    setTimeout(function () {
        checkConnection();
    }, 50);
},

function (error) {
    console.log("download error source " + error.source);
    console.log("download error target " + error.target);
    console.log("upload error code: " + error.code);
});
},
fail);
},
fail);

【讨论】:

    【解决方案2】:

    其实,

    encodeURI("https://dl.dropbox.com/u/13253550/db02.xml") === "https://dl.dropbox.com/u/13253550/db02.xml"
    

    所以你的解决方案必须有另一个因素;)。我在升级时遇到了同样的问题。正如file plugin upgrade notes 提到的那样,fileEntry.toURL() 似乎是解决方案。

    为了保护您的代码在以后免受这种情况的影响,请不要使用

    fileSystem.root.getFile(
      "dummy.html", {
    ...
    var sPath = fileEntry.toURL().replace("dummy.html", "");
    ...
    fileTransfer.download(
      DBuri,
      sPath + "database.xml"
    

    。而是直接去

    fileSystem.root.getFile(
      "database.xml", {
    ...
    fileTransfer.download(
      DBuri,
      fileEntry.toURL()
    

    在转换平台特定的 url 时,让 cordova/phonegap 来做这件事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2017-01-05
      • 2016-11-08
      • 2018-10-10
      相关资源
      最近更新 更多