如何使用Cordova删除文件?

Ida*_*dar 1 cordova ibm-mobilefirst

注意:此问题正在重新发布,因为无论出于何种原因,原始发布者决定在提供并接受答案后将其删除。因此,我再次添加它以保留知识


原始问题:

我试图了解cordova appache的原始操作如何工作。我在worklight中创建了一个delete函数,如下所示:

index.html:

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

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);
}
Run Code Online (Sandbox Code Playgroud)

尝试删除时,它不是在删除代码。我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

我可以帮忙吗?谢谢。

Ida*_*dar 5

您不能简单地拥有一个包含文件路径并.remove在其上使用方法的变量。出于所有目的和目的,它只是一个带有一些字符串的变量。这基本上就是错误的意思。它不知道是什么.remove

.remove只有在您拥有对文件系统的访问权限后,该选项才可用。
以下作品:

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.');
            }
        ); 
    } 
); 
Run Code Online (Sandbox Code Playgroud)

由于这是先前提出的问题的继续,因此这里是完整的示例:

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>
Run Code Online (Sandbox Code Playgroud)

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.');
            }); 
        } 
    ); 
}
Run Code Online (Sandbox Code Playgroud)