How to save a file locally using Apache Cordova 3.4.0

I had a problem saving the file locally on an iOS (or Android) device using the apache cordova "file" plugin. The problem, I believe, sets the path correctly.

this is the error message I get from Xcode Failed to create a path to save the downloaded file. Operation cann \ U2019t has been completed. (Cocoa error 512.)

Here is the code where I try 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> 
+7
android ios cordova file-transfer
source share
3 answers

The file path contains a typo (or grammatical error):

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

You must write it down as permanent .

The correct code is:

 var fileURL = "cdvfile://localhost/persistent/file.png"; 
+14
source share

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

The first and second links provide you with information about the plugin file and how to install it.

The third shows how to use the File plugin.

Every time you need to do something with Cordova, check if the plugin is available for this :)

Sincerely.

+1
source share

So far I have only tested this on Android, but I believe that it should work as is or with minor changes in 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) }) }) } ) }) 

Please note that I also need the contents of the file, so the bit at the end can be omitted if you do not need the contents at the time of loading.

Also note that there is a much simpler method using window.saveAs, but it is only available in Android 4.4.

0
source share

All Articles