未捕获的TypeError:参数"源"的类型错误FileTransfer.download:预期字符串,但未定义。

时间:2015-07-01 11:56:29

标签: javascript cordova amazon-s3

我最近问了一个关于在手机上本地下载和存储文件的问题。早期的问题是我无法访问LocalFileSystem。问题解决了,但现在我遇到了一个新的错误。我之前没有看到过这个错误,也无法理解。我到目前为止的代码:

 function storeFile(bucketName, csvfile){
     s3Client.getBucketLocation(params = {Bucket: bucketName},function(err,data){
    if(err) console.log("Error :: Cannot retrieve bucket location", err);
    else {
      //console.log(data);
      region = data.LocationConstraint;
      url = "https://s3-" + region + ".amazonaws.com/" + bucketName + "/" + csvfile;  
      //alert(url);
    }
  });

  window.requestFileSystem(LocalFileSystem.PERSISTENT,0, 
    function(fs){
      //alert(fs.root.fullPath); 
      fs.root.getDirectory("Amazedata", {create: true}, 
        function(d){
          //console.log("got dir");
          filePath = d.fullPath + csvfile;
          //alert(filePath);
          fileTransfer = new FileTransfer();
          console.log('downloading to: ', filePath);
          fileTransfer.download(url, filePath, 
            function (entry) {
              console.log(entry.fullPath); // entry is fileEntry object
            }, 
            function (error) {
              console.log("Some error", error.code);
          });
      }, onError);
  }, onRequestError);
}`

在上面的代码中,我能够提取区域,并能够访问和创建文件夹。问题是,下载时,它会给我错误:

Uncaught TypeError: Wrong type for parameter "source" of FileTransfer.download: Expected String, but got Undefined.

1 个答案:

答案 0 :(得分:1)

您的S3呼叫是异步的。在该调用完成之前,不会定义url值。将下载代码移至.getBucketLocation回调。

function storeFile(bucketName, csvfile) {
    s3Client.getBucketLocation(params = {
        Bucket: bucketName
    }, function(err, data) {
        if (err) console.log("Error :: Cannot retrieve bucket location", err);
        else {
            //console.log(data);
            var region = data.LocationConstraint;
            var url = "https://s3-" + region + ".amazonaws.com/" + bucketName + "/" + csvfile;
            //alert(url);

            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
                function(fs) {
                    //alert(fs.root.fullPath); 
                    fs.root.getDirectory("Amazedata", {
                            create: true
                        },
                        function(d) {
                            //console.log("got dir");
                            var filePath = d.fullPath + csvfile;
                            //alert(filePath);
                            var fileTransfer = new FileTransfer();
                            console.log('downloading to: ', filePath);
                            fileTransfer.download(url, filePath,
                                function(entry) {
                                    console.log(entry.fullPath); // entry is fileEntry object
                                },
                                function(error) {
                                    console.log("Some error", error.code);
                                });
                        }, onError);
                }, onRequestError);
        }
    });

}