apache / cordova-plugin-file

Apache Cordova File Plugin
https://cordova.apache.org/
Apache License 2.0
741 stars 756 forks source link

Android works, but iOS 13.5.1 returns error code 1 #396

Closed caleb87 closed 3 years ago

caleb87 commented 3 years ago

Bug Report

Reading a file works on Android. It fails on iOS 13.5.1 with error code 1.

The code I'm using:

function getLocalFile(url, cb) {
    console.log("ATTEMPTING TO LOAD: ",cordova.file.applicationDirectory + "www/"+url);
    window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + "www/"+url, function(fileEntry) {
        console.log(fileEntry);
        fileEntry.file(function(file) {
            var s = "";
            s += "<b>name:</b> " + file.name + "<br/>";
            s += "<b>localURL:</b> " + file.localURL + "<br/>";
            s += "<b>type:</b> " + file.type + "<br/>";
            s += "<b>lastModifiedDate:</b> " + (new Date(file.lastModifiedDate)) + "<br/>";
            s += "<b>size:</b> " + file.size + "<br/>";
            console.log(s);
            var reader = new FileReader();
            reader.onloadend = function() {
                var fileContents = this.result;
                console.log("Successful file read: "+fileContents);
                if(cb) cb(fileContents);
            };
            reader.readAsText(file);
        });
    }, function(err) {
        console.log("File failed ", err);
        console.dir(err);
    });
}

Also works on Android, but fails on iOS:

function getLocalFile(url, cb) {

    resolveLocalFileSystemURL(cordova.file.applicationDirectory, function(entry) {
        console.log("Success! Got a app dir");
        console.log(entry);
        var nativeURL = entry.nativeURL;
        console.log("NATIVE URL IS ",nativeURL);

        console.log("ATTEMPTING TO LOAD: ",nativeURL + "www/"+url);
        window.resolveLocalFileSystemURL(nativeURL + "www/"+url, function(fileEntry) {
            console.log("FILE ENTRY INFORMATION IS: ");
            console.log(fileEntry);
            fileEntry.file(function(file) {
                var s = "";
                s += "<b>name:</b> " + file.name + "<br/>";
                s += "<b>localURL:</b> " + file.localURL + "<br/>";
                s += "<b>type:</b> " + file.type + "<br/>";
                s += "<b>lastModifiedDate:</b> " + (new Date(file.lastModifiedDate)) + "<br/>";
                s += "<b>size:</b> " + file.size + "<br/>";
                console.log(s);
                var reader = new FileReader();
                reader.onloadend = function() {
                    var fileContents = this.result;
                    console.log("Successful file read: "+fileContents);
                    if(cb) cb(fileContents);
                };
                reader.readAsText(file);
            });
        }, function(err) {
            console.log("File failed ################# ", err);
            console.dir(err);
        });

    }, function(error) {
        console.error("Something bad happened, and we didn't get a app dir");
    });

}

What is expected to happen?

Both platforms return same result.

What does actually happen?

Returns error code 1.

Environment, Platform, Device

Physical iPhone X, iOS 13.5.1

Version information

cordova-plugin 6.0.2 Phonegap 9 iOS 13.5.1 Xcode for building

Checklist

KFCVme50-CrazyThursday commented 3 years ago

hello, boy . I meet this bug too, Did you figure out this problem , my friends help me @caleb87

bhumboldt commented 3 years ago

Hey @caleb87 did you ever get this resolved? Running into the same thing on iPadOS 13.7

wannadream commented 3 years ago

@caleb87 I ran into the same issue, do you have a solution?

mirko77 commented 3 years ago

same issue

JaosnHsieh commented 2 years ago

Is this specific to iOS 13? I can write file successfully to iOS 12 by the code here

cgeorg commented 2 years ago

Why is this marked Closed?

SmallBlueE commented 6 months ago

url must be under the app's folder. if not, resolveLocalFileSystemURL() returns error 1