简体   繁体   English

将文件从应用程序文件夹复制到JQM / Phonegap App中的根文件夹?

[英]Copy file from application folder to root folder in JQM/Phonegap App?

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. 我已经看到了几个不同的示例,说明如何使用Phonegap File API将文件从一个目录复制到另一个目录,但是无法将文件从我的应用程序文件夹复制到根目录中的现有文件夹。 The File API documentation is really vague and I keep getting file not found in the application directory. File API文档非常模糊,我一直在应用程序目录中找不到文件。

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. 我可以使用root.toURL()访问sdcard根文件夹文件:/// storage / emulated / 0 /并读取和写入它,我似乎无法访问我的应用程序文件夹。

permission in Phonegap Build config file. Phonegap Build配置文件中的权限。

<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 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);
}

You can use FileTransfer to download and store file to different location: 您可以使用FileTransfer将文件下载并存储到不同的位置:

// 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. 正如我最近发现的,这不适用于Windows 81,但我在Android和iOS上成功测试了它。

Looks like you're wanting to do this on both Android and iOS; 看起来你想要在Android和iOS上都这样做; things work a bit differently on each platform. 每个平台上的工作方式都有所不同。 Using the cordova-plugin-file plugin: 使用cordova-plugin-file插件:

o The alias for the Android application folder ( file:///android_asset/ ) is cordova.file.applicationDirectory . o Android应用程序文件夹( file:///android_asset/ )的别名是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您可能已经知道,Android SD卡根目录( file:///storage/emulated/0/ )的cordova.file.externalRootDirectorycordova.file.externalRootDirectory

o The alias for the iOS application folder ( appname.app/ ) is cordova.file.applicationDirectory . o iOS应用程序文件夹( appname.app/ )的别名是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. 显然,在iOS上没有可写入的外部存储空间,但根据您要对文件执行的操作以及需要访问它的人员,您可以将其写入文档( Documents/ - > cordova.file.documentsDirectory )或库( Library/NoCloud/ - > cordove.file.dataDirectory )文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM