Idan Adar
Idan Adar

Reputation: 44526

How to delete a file using Cordova?

Note: this question is being re-posted because for whatever reason the original poster decided to remove it after an answer was provided and accepted. I am therefore adding it again to preserve the knowledge


Original question:

I'm trying to understand how the crud operations for cordova appache works. I created a delete function in worklight as follows:

index.html:

<a href="#" class="btn large" onclick="deleteAudio();">Delete the local MP3 file</a><br/>

main.js:

function deleteAudio() {
  var entry= "file:///data/data/com.TestApp/files/4638.mp3";
  function success(entry) {
    alert("Removal succeeded");
 }

  function fail(error) {
    alert('Error removing file: ' + error.code);
 }

// remove the file
entry.remove(success, fail);
}

when trying to delete, it is not deleting the code. i'm getting this error:

10-11 09:54:14.419: E/NONE(1821): Uncaught Exception: Uncaught TypeError: Object file:///data/data/com.TestApp/files/4638.mp3 has no method 'remove' at (compiled_code):68

Can i have any help please? Thank you.

Upvotes: 1

Views: 1308

Answers (1)

Idan Adar
Idan Adar

Reputation: 44526

You cannot simply have a variable that contains a path to a file and use the .remove method on it. For all intents and purposes it's just a variable with some string inside it. That's basically what the error is saying. It does not know what .remove is.

.remove will be available only after you have gained access to the filesystem.
The following works:

var entry= "file:///data/data/com.TestApp/files/4638.mp3";

window.resolveLocalFileSystemURL (entry, 
    function (fileEntry) { 
        fileEntry.remove(
            function () { 
                alert('File is removed.'); 
            }, 
            function (error) {
                alert('Unable to remove file.');
            }
        ); 
    } 
); 

Since this continues the previously asked question, here is the full example:

index.html

<button id="downloadMP3">Download MP3 file</button><br/>
<button id="playMP3" disabled>Play MP3 file</button><br/>
<button id="stopMP3" disabled>Stop MP3 file</button><br/>           
<button id="deleteMP3" disabled>Delete MP3 file</button>

main.js

var mediaFile;
var mediaPlayback;

function wlCommonInit(){
    $("#downloadMP3").click(downloadMP3);
    $("#playMP3").click(playMP3);
    $("#stopMP3").click(stopMP3);    
    $("#deleteMP3").click(deleteMP3);
}

function downloadMP3() {
    var fileTransfer = new FileTransfer();
    var remoteFilePath = encodeURI("http://www.noiseaddicts.com/samples_1w72b820/4638.mp3");
    var localDownloadPath = cordova.file.dataDirectory + '4638.mp3';

    alert ("Downloading...");
    fileTransfer.download(
        remoteFilePath,
        localDownloadPath,
        function(successResponse) {
            mediaFile = successResponse.toURL();
            // Remove "file://" so file could be found and later played.
            mediaFile = mediaFile.replace('file://','');
            $('#playMP3').prop('disabled', false);
            $('#stopMP3').prop('disabled', false);            
            $('#deleteMP3').prop('disabled', false);
        },
        function(errorResponse) {
            alert (JSON.stringify(errorResponse));
        }
    );
}

function playMP3() {
    mediaPlayback = new Media(
            mediaFile,
            function() {
                alert("Finished playing audio file.");
            },
            function() {
                alert("Failed playing audio file.");
            }
        );

    mediaPlayback.play();
}

function stopMP3() {
    mediaPlayback.stop();
}

function deleteMP3() {
    // Put back "file://" since it is needed in order to be found.
    mediaFile = "file://" + mediaFile;

    window.resolveLocalFileSystemURL(mediaFile, 
        function (fileEntry) { 
            fileEntry.remove(
                function () { 
                alert('File is removed.'); 
            }, 
            function (error) {
                alert('Unable to remove file.');
            }); 
        } 
    ); 
}

Upvotes: 6

Related Questions