2015-07-01 65 views
1

我最近问了一个关于在手机上本地下载和存储文件的问题。较早的问题是我无法访问LocalFileSystem。问题解决了,但现在我面临着一个新的错误。我以前没有看到过这个错误,也无法理解它。我到目前为止的代码:未捕获的类型错误:FileTransfer.download的参数“源”的类型错误:期望的字符串,但未定义。

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.

+0

可能这些值中的某个没有定义:'region,bucketName,csvfile' –

+0

所有的值都被定义。我能够将这些变量的内容打印到控制台上。 –

+0

我面临同样的问题/错误消息,但仅限于某些文件。当我尝试从URL下载pdf文件时:http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf一切正常,但是当我尝试从我的Alfresco下载PDF文件时DMS服务器,它失败。不过,在这两种情况下,响应内容类型都是相同的:“application/pdf”。你有没有解决这个问题呢? –

回答

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

} 
+0

尝试了你的建议。我正在获取源和目标错误。 '下载错误源https:// s3-us-west-2.amazonaws.com/bucketname /文件名'和'下载错误目标/ Amazedata /文件名' –

+0

@PallaviPrasad我不能测试你的代码。你原来的代码的基本问题是异步S3调用;可能还有其他问题。 – Pointy