gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 16:01:46 24 4
gpt4 key购买 nike

我见过几个不同的示例,说明如何使用 Phonegap 文件 API 将文件从一个目录复制到另一个目录,但在将文件从我的应用程序文件夹复制到根目录中的现有文件夹时遇到问题。文件 API 文档非常模糊,我一直在应用程序目录中找不到文件。

我可以使用 root.toURL() 访问 sdcard 根文件夹 file:///storage/emulated/0/并对其进行读写,但我似乎无法访问我的应用程序文件夹。

Phonegap 构建配置文件中的权限。

<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" />

任何帮助或例子都会很棒。

谢谢,

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

最佳答案

您可以使用 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);
}
);

我最近发现,这在 Windows 81 上不起作用,但我在 Android 和 iOS 上成功测试了它。

关于javascript - 将文件从应用程序文件夹复制到 JQM/Phonegap App 中的根文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29479060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com