Search code examples
javascriptcordovaamazon-s3cordova-plugins

Uncaught Reference Error: LocalFileSystem not defined


This question has been asked before on this forum but I could not find a satisfactory solution to the problem. Hence the question again. I am trying to download and store a file from the internet into the local file system on my phone. I am getting the following error:

Uncaught ReferenceError: LocalFileSystem is not defined

The code I have so far is:

function storeFile(bucketName, csvfile){
   var region;
   s3Client.getBucketLocation(params = {Bucket: bucketName},function(err,data){
       if(err) console.log("Error :: Cannot retrieve bucket location", err);
       else {
          console.log(data);
          region = data;
       }
    });

   var url = "http://s3-"+region+".amazonaws.com/"+bucketName+"/"+csvfile;
   window.requestFileSystem(LocalFileSystem.PERSISTENT,0,
     function(fs){
       filePath = fs.root.fullPath + "/" + csvfile;
       fileTransfer = new FileTransfer();
       fileTransfer.download(url, filePath, 
        function (entry) {
          console.log(entry.fullPath); // entry is fileEntry object
        }, 
        function (error) {
           console.log("Some error");
        });
    }, function(err){
       console.log("Request File System failure");
   });
}

Do I need to add any plugins into my app? I already have the File and FileTransfer plugins added. By the way this function is called after onDeviceReady is fired.

EDITED: I am not working with AngularJs. My code is on NodeJs. Secondly, I have no dependencies like the ionic framework etc. which could possibly present a problem. This same code has been tested on other data which have been successfully downloaded and stored. The problem has occurred after I changed the code to download from a specific location on the amazon server. If someone could address this problem for me, it would be great. I am able to access the bucket and the region through code. The only problem is the LocalFileSystem.


Solution

  • Was able to solve the problem. Just removed and re-added the file and file-transfer plugins.

    cordova plugin rm org.apache.cordova.file and cordova plugin rm org.apache.cordova.file-transfer to remove.

    cordova plugin add org.apache.cordova.file and cordova plugin add org.apache.cordova.file-transfer to add.

    It may tell you that these plugins are soon to be deprecated or something like that. You also have the option of using the later versions. To add you can use

    cordova plugin add cordova-plugin-file and cordova plugin add cordova-plugin-file-transfer. Just check if they are available under the respective builds in the platforms folder.