I am downloading a file to the local file system. I can successfully create the empty file via fileSystem.root.getFile but fileTransfer.download fails with FILE_NOT_FOUND_ERR even though it's using the same path.

The problem is that my file is being created at //sdcard/MyDir/test.pdf (I confirmed using adb shell) but the fileEntry returned a path without sdcard: //MyDir/test.pdf. fileTransfer.download fails with this path. It also fails with the relative path MyDir/test.pdf.

If I hardcode the full path with 'sdcard' in it I can avoid the FILE_NOT_FOUND_ERR (specifically, in FileTransfer.java the resourceApi.mapUriToFile call succeeds) but then I get a CONNECTION_ERR and the console shows "File plugin cannot represent download path". (In FileTransfer.java, the filePlugin.getEntryForFile call returns null. I assume it doesn't like 'sdcard' in the path.)

Is there a better way to specify the target path in fileTransfer.download?

var downloadUrl = "http://mysite/test.pdf";
var relativeFilePath = "MyDir/test.pdf";  // using an absolute path also does not work

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
   fileSystem.root.getFile(relativeFilePath, { create: true }, function (fileEntry) {
      console.log(fileEntry.fullPath);  // outputs: "//MyDir/test.pdf"

      var fileTransfer = new FileTransfer();
      fileTransfer.download(
         downloadUrl,

         /********************************************************/
         /* THE PROBLEM IS HERE */
         /* These paths fail with FILE_NOT_FOUND_ERR */
         //fileEntry.fullPath,      // this path fails. it's "//MyDir/test.pdf"
         //relativeFilePath,        // this path fails. it's "MyDir/test.pdf"

         /* This path gets past the FILE_NOT_FOUND_ERR but generates a CONNECTION_ERR */
         "//sdcard/MyDir/test.pdf"
         /********************************************************/

         function (entry) {
            console.log("Success");
         },
         function (error) {
            console.log("Error during download. Code = " + error.code);
         }
      );
   });
});

I'm using the Android SDK emulator if that makes a difference.

有帮助吗?

解决方案

I was able to resolve this by using a URI for the file's path. (I also removed the unnecessary call to fileSystem.root.getFile per @Regent's comment.)

var downloadUrl = "http://mysite/test.pdf";
var relativeFilePath = "MyDir/test.pdf";  // using an absolute path also does not work

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
   var fileTransfer = new FileTransfer();
   fileTransfer.download(
      downloadUrl,

      // The correct path!
      fileSystem.root.toURL() + '/' + relativeFilePath,

      function (entry) {
         console.log("Success");
      },
      function (error) {
         console.log("Error during download. Code = " + error.code);
      }
   );
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top