[ngCordova] Move File

When move file in ngCordova or Cordova, it will need 2 plugins.

  1. cordova plugin add org.apache.cordova.file
  2. cordova plugin add org.apache.cordova.file-transfer

basic usage.

1
2
3
4
5
6
7
$cordovaFile.downloadFile(source, filepath, true, {}).then(function(result) {
// Success!
}, function(err) {
// Error
}, function(progress) {
// constant progress updates
});

it’s same as

1
2
3
4
5
6
7
8
9
10
var fileTransfer = new FileTransfer();
var uri = encodeURI(source);
fileTransfer.download(uri,filePath,
function (entry) {
// success
},
function (error) {
// error
},
trustAllHosts, options);

if you want to use it with $cordovaCamera.getPicture(), you will need to resolve FILE_URI first, and use it as source.

1
2
3
4
5
6
7
8
9
function getImageFileName(image) {
window.resolveLocalFileSystemURL(image,
function(entry) {
var uri = entry.toURL();
entry.file(function(file) {
var fileName = file.name;
});
});
}

API Document#File Entry

another problem is filepath. basePath can find it by below code. and filepath need a filename at the end.

1
2
3
4
5
6
$cordovaFile.createDir(directory, false).then(function(entry) {
// Success!
alert(entry.toURL());
}, function(err) {
// An error occured. Show a message to the user
});