【问题标题】:PhoneGap FileTransfer.download expects different path than FileSystem providesPhoneGap FileTransfer.download 需要不同于 FileSystem 提供的路径
【发布时间】:2014-04-09 00:56:34
【问题描述】:

我正在将文件下载到本地文件系统。我可以通过 fileSystem.root.getFile 成功创建空文件,但 fileTransfer.download 失败并显示 FILE_NOT_FOUND_ERR 即使它使用相同的路径。

问题是我的文件是在//sdcard/MyDir/test.pdf 创建的(我确认使用 adb shell)但是 fileEntry 返回了一个路径没有 sdcard://MyDir/test.pdf。 fileTransfer.download 使用此路径失败。它也因相对路径 MyDir/test.pdf 而失败。

如果我用 'sdcard' 对完整路径进行硬编码,我可以避免 FILE_NOT_FOUND_ERR(具体来说,在 FileTransfer.java 中,resourceApi.mapUriToFile 调用成功)但随后我得到一个 CONNECTION_ERR 和控制台显示“文件插件不能代表下载路径”。 (在 FileTransfer.java 中,filePlugin.getEntryForFile 调用返回 null。我认为它不喜欢路径中的 'sdcard'。)

有没有更好的方法在 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);
         }
      );
   });
});

如果有什么不同,我正在使用 Android SDK 模拟器。

【问题讨论】:

  • 尝试用“fileSystem.root.fullPath + '/' + relativeFilePath”替换你的目标文件名。另外,如果你想下载并保存文件,在下载之前创建它是没有用的。
  • @Regent fileSystem.root.fullPath 是 /,所以没有帮助。

标签: android cordova phonegap-plugins file-transfer android-file


【解决方案1】:

我能够通过使用文件路径的 URI 来解决此问题。 (我还根据@Regent 的评论删除了对 fileSystem.root.getFile 的不必要调用。)

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

【讨论】:

  • 谢谢它帮了我很多:)
  • 我已经完成了上述代码,但我仍然收到 FileTransferError..CONNECTION_ERR。我正在设备上运行。 jsfiddle.net/desmond80in/zamh9qLL/1
猜你喜欢
  • 1970-01-01
  • 2021-08-12
  • 2015-09-29
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 2023-04-02
  • 2011-10-07
相关资源
最近更新 更多