5

我最近将我的 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);
4

2 回答 2

6

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

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

原来我没有在我的下载源网址上做 encodeURI() (doh)

所以完整的工作代码:

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);
于 2014-03-06T06:50:13.053 回答
1

实际上,

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

所以你的解决方案必须有另一个因素;)。我在升级时遇到了同样的问题。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 来做这件事。

于 2014-04-06T16:16:17.017 回答