RohitK
RohitK

Reputation: 1494

Error in downloading file in Phonegap iOS

Hi I am using Phonegap to create iOS application. I am using Filetransfer API to download the image file. I am using following code to download file

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

When I run it on iOS device i am getting error:

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

How do i solve this?

Upvotes: 2

Views: 374

Answers (1)

Yasuharu  Suzuki
Yasuharu Suzuki

Reputation: 21

This is complex bug between cordova-plugin-file-transfer and cordova-plugin-file plugins.
FileTransfer plugin is using type "root" to File plugin. It is default, and can not change any types.
The path of "root" is "/", this is the cause of problem.
For example, the path of "persistent" is "/user/{your name}/Library/..." on iphone simulator.
You can not access "/".

I tried to modify @"root" path in getAvailableFileSystems() method, CDVFile.m of CordovaLib.xcodeproj.
However, app crashed after setting it.
There is no smart way, i decided to take the way changing string unnaturally.

download() method in CDVFileTransfer.m Line 440

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

add code under this.

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

Upvotes: 1

Related Questions