I\'ve been trying to copy file named versions.txt from applicationDirectory to externalApplicationStorageDirectory using cordova but code fails.
here is the code
now it works, target directory should be resolved.
here is the solution
window.resolveLocalFileSystemURL(cordova.file.externalApplicationStorageDirectory,
function onSuccess(dirEntry)
{
//alert(JSON.stringify(dirEntry));
fileEntry.copyTo(dirEntry, 'versions.txt',
function()
{
alert('copying was successful')
},
function()
{
alert('unsuccessful copying')
});
}, null);
The fileEntry.copyTo somehow always gave error, so I tried the fileEntry.moveTo as below, which works well for me in copying any file from the www into say the Library folder of the iOS device.
function copyToLocation(fileName){
console.log("Copying :"+fileName);
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory+"www/"+fileName,function (fileEntry)
{
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,function (directory)
{
fileEntry.moveTo(directory, 'new_fileName.txt',function(){
alert('Successful Copy!');
},
function()
{
alert('Copying Unsuccessful ');
});
},null);
}, null);
}
I finally got a functioning broader no specific/case function to copy a file having a baseFileURI
to a temporary (fileSystem == LocalFileSystem.TEMPORARY
) or persistent (fileSystem == LocalFileSystem.PERSISTENT
) APP directory, the destiny file having the name destPathName
.
It uses the cordova-plugin-file plugin.
function copyFile(baseFileURI, destPathName, fileSystem){
window.resolveLocalFileSystemURL(baseFileURI,
function(file){
window.requestFileSystem(fileSystem, 0,
function (fileSystem) {
var documentsPath = fileSystem.root;
console.log(documentsPath);
file.copyTo(documentsPath, destPathName,
function(res){
console.log('copying was successful to: ' + res.nativeURL)
},
function(){
console.log('unsuccessful copying')
});
});
},
function(){
console.log('failure! file was not found')
});
}
Use this function, for example, like this
copyFile("file:///storage/emulated/0/Android/data/com.form.parking.violation/cache/IMG-20180505-WA0004.jpg",
"myImg.jpg",
LocalFileSystem.TEMPORARY);
Served my source code to the PhoneGap developer app. My file copy code is below.
//COPY FILE
var wwwDirEntry;
//resolve url for directory entry for putting in copied file
window.resolveLocalFileSystemURL(cordova.file.dataDirectory+'phonegapdevapp/www/', function success(dirEntry) {
wwwDirEntry = dirEntry;
});
//resolve file URL to file entry to enable copying
//
//Desired URL: file:///data/user/0/com.adobe.phonegap.app/files/phonegapdevapp/www/my_awesome_file.doc
//BASE URL: cordova.file.dataDirectory / file:///data/user/0/com.adobe.phonegap.app/files/
//
//alert(JSON.stringify(cordova.file.dataDirectory));
//
window.resolveLocalFileSystemURL(cordova.file.dataDirectory+'phonegapdevapp/www/my_awesome_file.doc',
function onSuccess(fileEntry)
{
//alert(JSON.stringify(fileEntry));
fileEntry.copyTo(wwwDirEntry, 'a_copy_of_my_awesome_file.doc',
function()
{
alert('copying was successful');
},
function()
{
alert('copying FAILED');
});
}, function (e) { alert(JSON.stringify(e)); });