7

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>
Nathan
  • 1,609
  • 4
  • 25
  • 42

3 Answers3

15

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";
ar34z
  • 2,609
  • 2
  • 24
  • 37
  • @ar34z: can you tell how i can access this file in ionic: `file:///storage/emulated/0/SmaartMedia/jellyfish.jpg` its showing that is not allowed – Rahul Sharma Sep 07 '16 at 10:51
0

Check out these links :

http://cordova.apache.org/docs/en/3.4.0/cordova_plugins_pluginapis.md.html#Plugin%20APIs https://github.com/apache/cordova-plugin-file/blob/dev/doc/index.md

http://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html#File

First and second links provide you information about the plugin File and how to install it.

The third one show you how to use the File plugin.

Everytime you need to do something with Cordova, check if a plugin is available to do it :)

regards.

bottus
  • 883
  • 2
  • 13
  • 32
  • Thank you for the links, I have the plugin installed and its functioning 90% of the way ... (its able to call the local file system CDVFile) and its attempting to save the file to the disk, but the line where I am creating the var fileURL does not seem to be correct ... Xcode is tossing the following error:File Transfer Error: Could not create path to save downloaded file: The operation couldn’t be completed. (Cocoa error 512.) – Nathan Mar 11 '14 at 21:10
  • Are you sure the path you want to save your file to in correct ? – bottus Mar 11 '14 at 21:12
  • As sure as I can be ... Its the directory in the sandbox where I want to save the file ... but it is not functioning ... – Nathan Mar 11 '14 at 21:13
  • Did you set the configuration as specified in the doc ? – bottus Mar 11 '14 at 21:16
  • @Nathan how did you get this working? I can download with Android no problem, but I get the error you mentioned on iOS. On Android my path is file:///storage/sdcard0/Android/data/com.my.app/cache/image.png and on iOS it's file:///var/mobile/Applications//Documents/image.png – trainoasis Jul 08 '14 at 09:58
  • @trainoasis The problem as stated above is that I am a terrible speller, I had persistant vs persistent. Once I correct that problem it worked fine. – Nathan Jul 08 '14 at 15:24
  • @Nathan I see... I thought there was something else. Thanks! – trainoasis Jul 09 '14 at 06:56
0

So far I have only tested this on Android, but I believe it should work as-is, or with little modification on IOS:

var url = 'example.com/foo'

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
    fileSystem.root.getFile('foo_file', {create: true, exclusive: false},
        function(file_entry){
            var ft = new FileTransfer()
            ft.download(url, file_entry.toURL(), function(fe){
                fe.file(function(f){
                    reader = new FileReader()
                    reader.onloadend = function(ev){
                        console.log('READ!', ev.target.result)
                    }
                    reader.readAsText(f)
                })
            })
        }
    )
})

Note that I also needed the contents of the file, so the bit at the end may be omitted if you don't need the contents at the time of downloading.

Also note that there is a far simpler method using window.saveAs but it's only available in Android 4.4.

Abram
  • 413
  • 1
  • 3
  • 13