【问题标题】:Error in downloading file in Phonegap iOS在 Phonegap iOS 中下载文件时出错
【发布时间】:2016-10-30 06:45:38
【问题描述】:

您好,我正在使用 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";
}

我该如何解决这个问题?

【问题讨论】:

    标签: ios cordova download phonegap-plugins file-transfer


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      相关资源
      最近更新 更多