2015-07-01 37 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