I have seen a couple different examples of how to copy files from one directory to another using the Phonegap File API but am having trouble copying a file from my application folder to an existing folder in the root directory. The File API documentation is really vague and I keep getting file not found in the application directory.
I can access the sdcard root folder file:///storage/emulated/0/ using root.toURL() and read and write to it, I just cant seem to access my application folder.
permission in Phonegap Build config file.
<gap:plugin name="org.apache.cordova.file" version="1.3.1" />
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />
Any help or examples would be great.
Thanks,
RG
wwwPath = cordova.file.applicationDirectory;
var fp = "temp/myfile.txt";
var fileDestPath = "tempFolder/";
function copyFile(){
var basePath = wwwPath+fp;
window.resolveLocalFileSystemURL(basePath, function(fileEntry){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
var copyToPath = fileSystem.root.toURL()+fileDestPath;
fileEntry.copyTo(copyToPath, 'newFileName.txt', function(){
alert("file copy success");
},fileCopyFail);
},fileCopyFail);
},fileCopyFail);
function fileCopyFail(error) {
alert(error);
}
I have seen a couple different examples of how to copy files from one directory to another using the Phonegap File API but am having trouble copying a file from my application folder to an existing folder in the root directory. The File API documentation is really vague and I keep getting file not found in the application directory.
I can access the sdcard root folder file:///storage/emulated/0/ using root.toURL() and read and write to it, I just cant seem to access my application folder.
permission in Phonegap Build config file.
<gap:plugin name="org.apache.cordova.file" version="1.3.1" />
<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />
Any help or examples would be great.
Thanks,
RG
wwwPath = cordova.file.applicationDirectory;
var fp = "temp/myfile.txt";
var fileDestPath = "tempFolder/";
function copyFile(){
var basePath = wwwPath+fp;
window.resolveLocalFileSystemURL(basePath, function(fileEntry){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
var copyToPath = fileSystem.root.toURL()+fileDestPath;
fileEntry.copyTo(copyToPath, 'newFileName.txt', function(){
alert("file copy success");
},fileCopyFail);
},fileCopyFail);
},fileCopyFail);
function fileCopyFail(error) {
alert(error);
}
Share
Improve this question
edited Apr 7, 2015 at 1:21
BGecko
asked Apr 6, 2015 at 20:23
BGeckoBGecko
2512 gold badges6 silver badges15 bronze badges
2 Answers
Reset to default 5You can use FileTransfer to download and store file to different location:
// get base url for index.html
var baseUrl = location.href.replace("/index.html", "");
var fp = "temp/myfile.txt";
var fileDestPath = "tempFolder/";
function copyFile(){
var sourceFilePath = baseUrl + "/" +fp;
var targetFilePath = fileSystem.root.toURL()+fileDestPath;
var ft = new FileTransfer();
ft.download(
sourceFilePath,
targetFilePath,
function(entry){
alert("file copy success")
},
function(error){
alert(error);
}
);
As I recently found out, this won't work on Windows 81, but I tested it successfully on Android and iOS.
Looks like you're wanting to do this on both Android and iOS; things work a bit differently on each platform. Using the cordova-plugin-file
plugin:
o The alias for the Android application folder (file:///android_asset/
) is cordova.file.applicationDirectory
.
o As you probably already know, the alias for the Android SD card root (file:///storage/emulated/0/
) is cordova.file.externalRootDirectory
.
o The alias for the iOS application folder (appname.app/
) is cordova.file.applicationDirectory
.
Obviously there's no external storage to write to on iOS, but depending on what you want to do with the file and who needs to access it you could write it to your documents (Documents/
-> cordova.file.documentsDirectory
) or library (Library/NoCloud/
-> cordove.file.dataDirectory
) folder.