Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store a file locally using Apache Cordova 3.4.0

I am having a problem storing a file locally on an iOS (or android) device using apache cordova's "file" plugin. The problem I believe is setting the path properly.

this is the error message I get from Xcode Could not create path to save downloaded file: The operation couldn\U2019t be completed. (Cocoa error 512.)

Here is the code where I am attempting to save the file locally:

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">


document.addEventListener("deviceready", onDeviceReady, false);

var root;


function onDeviceReady(){
    // Note: The file system has been prefixed as of Google Chrome 12:
    window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onInitFs, errorHandler);
}

function onInitFs(fs) {


    var fileURL = "cdvfile://localhost/persistant/file.png";

    var fileTransfer = new FileTransfer();
    var uri = encodeURI("http://upload.wikimedia.org/wikipedia/commons/6/64/Gnu_meditate_levitate.png");

    fileTransfer.download(
            uri,
            fileURL,
            function(entry) {
                console.log("download complete: " + entry.fullPath);
            },
            function(error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code" + error.code);
            },
            false,
            {
                headers: {
                    "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
                }
            }
    );
}


function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  alert('Error: ' + msg);
}

</script>
like image 853
Nathan Avatar asked Mar 11 '14 20:03

Nathan


People also ask

Does LocalStorage work in Cordova?

LocalStorage. Local storage provides simple, synchronous key/value pair storage, and is supported by the underlying WebView implementations on all Cordova platforms.

Where is the file saved by Cordova file plugin on iOS?

There are two valid locations to store persistent files on an iOS device: the Documents directory and the Library directory. Previous versions of the plugin only ever stored persistent files in the Documents directory.


1 Answers

Your file path contains a typo (or a grammar error):

var fileURL = "cdvfile://localhost/persistant/file.png";

You should write it as persistent.

Correct code:

var fileURL = "cdvfile://localhost/persistent/file.png";
like image 149
ar34z Avatar answered Sep 30 '22 04:09

ar34z