OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Phonegap iOS 中下载文件时出错 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 17:12
标题: ios - 在 Phonegap iOS 中下载文件时出错

您好,我正在使用 Phonegap 创建 iOS 应用程序。我正在使用 Filetransfer API 下载图像文件。我正在使用以下代码下载文件

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, createFolder, null);

     function createFolder(fileSystem) {
       console.log('gotFS');
       fileSystem.root.getDirectory("MyDirectory", {
                            create: true,
                            exclusive: false
                            }, MyDirectorySuccess, MyDirectoryfail);
    }

    function MyDirectorySuccess(entry) {
         console.log('gotDirectorySuccess');
         downloadFile(entry);
    }
    function downloadFile(entry)
    {
        console.log('downloadFile');

        var filePath = entry.fullPath+'test.jpg';
        var fileTransfer = new FileTransfer();
        var url = 'https://www.facebookbrand.com/img/fb-art.jpg';

        fileTransfer.download(
                         url, filePath, function(entry) {
                         console.log("success");
                         }, function(error) {
                         console.log("error");
                         }, true, {});
    }

当我在 iOS 设备上运行它时出现错误:

    FileTransferError {
    body = "Could not create path to save downloaded file: You don\U2019t have permission to save the file \U201cMyRecipeDirectory\U201d in the folder \U201cMonarch13A452.J72OS\U201d.";
    code = 1;
    "http_status" = 200;
    source = "https://www.facebookbrand.com/img/fb-art.jpg";
    target = "cdvfile://localhost/root/MyDirectory/test.jpg";
}

我该如何解决这个问题?



Best Answer-推荐答案


这是 cordova-plugin-file-transfer 和 cordova-plugin-file 插件之间的复杂错误。
FileTransfer 插件正在使用“root”类型的文件插件。为默认值,不能更改任何类型。
“root”的路径是“/”,这就是问题的原因。 例如,“persistent”的路径在iphone模拟器上为“/user/{your name}/Library/...”。
您无法访问“/”。

我尝试在 getAvailableFileSystems() 方法中修改 @"root"路径,CordovaLib.xcodeproj 的 CDVFile.m。
但是设置后app崩溃了。
没有聪明的办法,我决定采取不自然的方式改变字符串。

CDVFileTransfer.m 第 440 行中的 download() 方法

target = [target stringByReplacingOccurrencesOfString"//" withString"/"];
targetURL = [[self.commandDelegate getCommandInstance"File"] fileSystemURLforLocalPath:target].url;

在此下添加代码。

NSString *absoluteURL = [targetURL absoluteString];
if ([absoluteURL hasPrefix"cdvfile://localhost/root/"]) {
      absoluteURL = [absoluteURL stringByReplacingOccurrencesOfString"cdvfile://localhost/root/" withString"cdvfile://localhost/persistent/"];
      targetURL = [NSURL URLWithString:absoluteURL];
}

关于ios - 在 Phonegap iOS 中下载文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38067751/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4