科尔多瓦删除sqlite后删除文件 [英] Cordova delete files after sqlite delete it
本文介绍了科尔多瓦删除sqlite后删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个应用程序,我可以在其中拍照或从图库中选择它,然后将其移动到文件系统中,将图像的imgpath保存到sqlite db中并显示它,到目前为止一切正常,现在,当我从中删除imgpath时sqlite db,我想同时从文件系统中删除它。
I have this app where I take picture or select it from gallery then move it into file-system , save the imgpath for the image into sqlite db and display it, everything works great so far , now when I delete the imgpath from the sqlite db , I want to delete it from file-system at the same time .
$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);
}
);
}
推荐答案
首先下载插件-cordova插件添加cordova-plugin-文件
First download the plugin - 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
这篇关于科尔多瓦删除sqlite后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文