【问题标题】:Cordova delete files after sqlite delete it科尔多瓦在sqlite删除后删除文件
【发布时间】:2017-02-15 18:42:54
【问题描述】:

我有这个应用程序,我可以在其中拍照或从图库中选择它,然后将其移动到文件系统中,将图像的 imgpath 保存到 sqlite db 并显示它,到目前为止一切正常,现在当我从sqlite db,我想同时从文件系统中删除它。

$scope.deleteImg = function(imgPath) {


    if (!imgPath) {
        console.error("No fileName specified. File could not be deleted.");
        return false;
    } else {


        $cordovaFile.checkFile(cordova.file.externalRootDirectory, imgPath.id).then(function() {
            $cordovaFile.removeFile(cordova.file.externalRootDirectory, imgPath.id).then(function(result) {
                console.log("image '" + imgPath + "' deleted", JSON.stringify(result));
            }, function(err) {
                console.error("Failed to delete file '" + imgPath.id + "'", JSON.stringify(err));

            });
        }, function(err) {
            console.log("image '" + imgPath.id + "' does not exist", JSON.stringify(err));
        });

    }

    $scope.add = function(path) { 
             
            console.log("I AM ADDING TO DB WITH PATH: " + path);   

              
            $cordovaSQLite.execute(db, "INSERT INTO imageTable (image) VALUES(?)", [path]).then(
                function(res) {
                    console.log("I AM DONE ADDING TO DB");   
                }
            );                


                  
        },
        function(e) {        
            alert(e);      
        };



        

      
    $scope.ShowAllData = function() {
        console.log("I AM READING FROM DB");   
        $scope.images = [];             
        $cordovaSQLite.execute(db,"SELECT * FROM imageTable ORDER BY id DESC"  ).then(        function(res) {

            console.log("I FINISHED READING  FROM DB");   

                      
            if (res.rows.length > 0) {            
                for (var i = 0; i < res.rows.length; i++) {              
                    $scope.images.push({
                        id: res.rows.item(i).id,
                        image: res.rows.item(i).image

                                      
                    });

                }          
            } 
            return $scope.images;

                    
        },         function(error) {          
            alert(error);        
        }      );

            
    }  

    $scope.getImgIDbyName = function(name) {

        console.log('[getImgIDbyName] - get image with name:  ' + name);
        var sql = "SELECT * FROM imageTable WHERE image = '" + name + "';";
        console.log(sql);
        $cordovaSQLite.execute(db,
            sql
        ).then(
            function(res) {
                if (res.rows.length > 0) {
                    if (res.rows.length > 1) {
                        console.log('[getImgIDbyName] - OOPS more than 1 image returned!!!!  ' + name);
                    } else {
                        $scope.delete(res.rows.item(0).id);
                        $scope.deleteImg(imagePath.id);
                    }
                } else {
                    console.log('[getImgIDbyName] - no image found with name:  ' + name);
                }
            },
            function(error) {
                alert("error occured: " + error.message);
            }
        );



    }

【问题讨论】:

    标签: angularjs sqlite cordova ionic-framework


    【解决方案1】:

    首先下载插件-cordova plugin add cordova-plugin-file

    然后执行以下代码

    var path = cordova.file.applicationDirectory + '/www/res/image'; // get the absolute path
    var filename = "image.png";
    
    window.resolveLocalFileSystemURL(path, function(dir) {
        dir.getFile(filename, {create:false}, function(fileEntry) {
                  fileEntry.remove(function(){
                      // The file has been removed succesfully
                  },function(error){
                      // Error deleting the file
                  },function(){
                     // The file doesn't exist
                  });
        });
    });
    

    这里是完整的文档:https://github.com/apache/cordova-plugin-file/#where-to-store-files

    【讨论】:

    • 感谢您的帮助,我已经安装了插件并使用它将图像保存到文件系统中,您可以在这里查看我的完整代码stackoverflow.com/questions/42109744/…
    • 从数据库中删除后,如何从文件系统中删除 imgpath,这就是我苦苦挣扎的地方,我需要将相同的 id 传递给文件系统的 deleteImg
    • 让我在这里看看你的代码。那么,您从 sqlite 获取该 ID/文件名有问题吗?
    • 已经看到你的代码了。因此,您将图像保存到数据目录。 cordova.file.dataDirectory,尝试检索数据目录中的所有文件,你会得到什么。
    • 您在错误的目录中。 $cordovaFile.removeFile(cordova.file.externalRootDirectory, imgPath.id)。请将其更改为 cordova.file.dataDirectory
    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多