首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Cordova删除文件?

如何使用Cordova删除文件?
EN

Stack Overflow用户
提问于 2015-10-25 05:41:33
回答 1查看 1.2K关注 0票数 1

注意:这个问题正在重新发布,因为无论出于什么原因,原海报决定在提供并接受答案后将其删除。因此,我再加上一次,以保存知识。

原始问题:

我想了解cordova appache的crud操作是如何工作的。我在worklight中创建了一个删除函数,如下所示:

index.html:

代码语言:javascript
复制
<a href="#" class="btn large" onclick="deleteAudio();">Delete the local MP3 file</a><br/>

main.js:

代码语言:javascript
复制
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);
}

尝试删除时,它不是删除代码。我得到了一个错误:

代码语言:javascript
复制
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

能帮我个忙吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-25 05:41:45

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

只有在您获得对文件系统的访问之后,.remove才可用。

以下工作:

代码语言:javascript
复制
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.');
            }
        ); 
    } 
); 

由于这将继续the previously asked question,下面是完整的示例:

index.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
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.');
            }); 
        } 
    ); 
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33326609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档