如何使函数与forEach循环检查2个文件,如果存在返回字符串,如果不返回文件名



我有一个应用程序可以在LocalFileSystem中下载并保存文件,我需要同时运行两个文件,在启动之前我需要检查这些文件是否存在。我做了一些这样的东西,但我喜欢它愚蠢的功能,它可以与控制台一起工作,但不能作为返回字符串工作

function fileExist(songName,vocalName){ // i change it a bit for this example
var downloadedFolder =  'filesystem:http://192.168.1.20:3000/persistent/downloaded/';
var fileName = [];
storedFiles = [];
misingFiles = [];
fileName[0] = ({'value':'Song','name':musicName}); 
fileName[1] = ({'value':'Vocal','name':vocalName});
fileName.forEach(function(item) {
let path = downloadedFolder + item.name;
const fName = item.value;
window.resolveLocalFileSystemURL(path, 
function(){
storedFiles.push( fName )
if ( misingFiles.length == 0 && storedFiles.length == 2) {
return 'All';
}
},
function(){
misingFiles.push( fName )
if ( misingFiles.length == 2 ){
return 'Nothing';
}
else if ( misingFiles.length == 1 && storedFiles.length == 1){
return misingFiles[0];
}
})
})}

我想让这个函数像这样使用:

if ( fileExist(songName,vocalName) == 'Nothing' )

感谢

如果使用NodeJS,可以使用fs检查文件是否存在。

if (!fs.existsSync(path)) {
// file doens't exists
} else {
// file does exists
}

如果你不使用NodeJS,你可以设置一个简单的localhost服务器,并向它发送一个请求,以检查它是否与fs一起存在。

如果您使用的是electron(idk您使用的框架(,则可以使用electron ipc将消息从主进程发送到渲染器进程。

您可以使用phonegap中的FileReader对象来检查文件是否存在。您可以检查以下内容:

var reader = new FileReader();
var fileSource = <here is your file path>
reader.onloadend = function(evt) {
if(evt.target.result == null) {
// If you receive a null value the file doesn't exists
} else {
// Otherwise the file exists
}         
};
// We are going to check if the file exists
reader.readAsDataURL(fileSource);   

如果这个确实有效,请查看这篇文章的评论:如何检查文件';在电话簿中是否存在phonegap(这是我从得到的答案(

最新更新