FileSystem on Cordova 3.4.0 fails “Could not create target file”

六月ゝ 毕业季﹏ 提交于 2019-12-04 08:19:56
Hessius

I found the documentation for both the file plugin ( link) and the fileTransfer plugin ( link)

After making the change noted in the original question, I wondered if the file plugin part was OK and started looking for discrepancies between my fileTransfer code and the examples provided.

Turns out I wasn't doing encodeURI() on my download source url (doh)

so the complete, working code:

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);

Actually,

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

so your solution must have another factor ;) . I had the same problem when upgrading. fileEntry.toURL() appeared to be the solution, just as file plugin upgrade notes mentioned.

To secure your code against this in future don't use

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

. instead go directly for

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

and let cordova/phonegap do the lifting when it comes to transforming platform specific urls.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!