Cordova 3.6.3在Android下载文件

时间:2014-10-21 07:47:57

标签: android file cordova

我正在使用Cordova 3.6.3,我想在Android中下载文件。我添加了有关文件和文件传输的所有权限,还添加了文件和文件传输插件。它给我错误代码:1。在内存上创建文件,但它唯一的名称。为空。问题出在哪里?

我查找其他解决方案,但它不起作用。

  var resimURL= "https://farm"+url.farm+".staticflickr.com/"+url.server+"/"+url.id+"_"+url.secret+"_b.jpg";
  //alert("Download");
  alert(resimURL);

  var remoteFile =resimURL;
  var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
      function(fileSystem) {
          fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, 
          function(fileEntry) {
              var localPath = fileEntry.fullPath;
              alert(localPath);
              if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
                  localPath =fileSystem.root.toUrl()+localPath.substring(7);
                  alert(localPath);
              }
              var ft = new FileTransfer();
              ft.download(remoteFile, localPath,
                  function(entry) {
                      alert("Download Complete");
                  },
              fail);
          },
      fail);
      },
  fail);
  function fail(error) {
      alert("error"+error.code);
  }

2 个答案:

答案 0 :(得分:0)

我认为你应该在将uri传递给下载方法之前对其进行编码:

var resimURL = encodeURI("your uri");

这在文档中描述如下:

  

source:要下载文件的服务器的URL,由encodeURI()编码。

希望这有帮助。

参考:https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md

答案 1 :(得分:0)

我做到了,现在正在工作..

var ft = new FileTransfer();
ft.download(remoteFile,
            "/sdcard/Pictures/"+url.id+"_"+url.secret+"_b.jpg",
            function(entry) {
                console.log("download complete: " + entry.toURL());
            }, fail);