Sidedcore
Sidedcore

Reputation: 193

Phonegap File transfer errors

I'm trying to get my PhoneGap app to create a directory on the mobile device, but i keep running into the same errors, which are a mixture of:

ENCODING_ERR - 5 - The URL is malformed. Make sure that the URL is complete and valid.

PATH_EXISTS_ERR - 12 - The file or directory with the same path already exists.

INVALID_MODIFICATION_ERR - 9 - The modification requested is not allowed. For example, the app might be trying to move a directory into its own child or moving a file into its parent directory without changing its name.

$( document ).on( 'click', '#A2', function () {
    downloadPhoto();
} );

function downloadPhoto() {
        alert( "Downloading" );
        window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, function systemSuccess( dirEntry ) {
            alert( "Download about to begin" );
            dirEntry.root.getDirectory( 'thisfilenamewillneverexist', { create: true }, function ( fileSystem  ) {
                alert( fileSystem );
            }, errorHandler );
        }, errorHandler );
    }

function errorHandler( e ) {
    var msg = '';
    switch ( e.code ) {
        case FileError.ENCODING_ERR:
            msg = 'ENCODING_ERR';
            break;
        case FileError.INVALID_MODIFICATION_ERR:
            msg = 'INVALID_MODIFICATION_ERR';
            break;
        case FileError.INVALID_STATE_ERR:
            msg = 'INVALID_STATE_ERR';
            break;
        case FileError.NO_MODIFICATION_ALLOWED_ERR:
            msg = 'NO_MODIFICATION_ALLOWED_ERR';
            break;
        case FileError.NOT_FOUND_ERR:
            msg = 'NOT_FOUND_ERR';
            break;
        case FileError.NOT_READABLE_ERR:
            msg = 'NOT_READABLE_ERR';
            break;
        case FileError.PATH_EXISTS_ERR:
            msg = 'PATH_EXISTS_ERR';
            break;
        case FileError.QUOTA_EXCEEDED_ERR:
            msg = 'QUOTA_EXCEEDED_ERR';
            break;
        case FileError.SECURITY_ERR:
            msg = 'SECURITY_ERR';
            break;
        case FileError.TYPE_MISMATCH_ERR:
            msg = 'TYPE_MISMATCH_ERR';
            break;
        default:
            msg = 'Unknown Error';
            break;
    };
    alert( 'Error: ' + msg );
}

Why or how does that file name already exist if its the first time I'm creating it?

Upvotes: 4

Views: 6252

Answers (2)

Rajan
Rajan

Reputation: 495

In successcallback of requestFileSystem you will be passed a "fileSystem" parameter which in simple terms is the mother (main Object/Parent) of File API.

If you do "fileSystem.root.getDirectory" in the successcallback you will be passed a parameter of "directoryEntry" which basically is the same of "fileSystem.root" therefore you can access all the methods listed under directoryEntry right away and don't need to use the root.

Upvotes: 1

Sidedcore
Sidedcore

Reputation: 193

Managed to fix the error, turns out you dont need to include "root" when creating a file in a newly created directory.

Code for anyone else stuck on this problem or something similar:

var folderDir;
var fileDir;
var FolderName = "OSMaps";

$( document ).on( 'click', '#A2', function () {
    window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, downloadPhoto, errorHandler);
} );

function downloadPhoto(fileSystem) {
    fileSystem.root.getDirectory( FolderName, { create: true, exclusive: false }, 
    function ( dirEntry ) {
        folderDir = dirEntry;
->      folderDir.getFile( 'test.jpg', { create: true, exclusive: false }, <-
        function ( fileEntry ) {
                fileDir = fileEntry;
        }, errorHandler );
}, errorHandler );

Upvotes: 5

Related Questions