PDFファイルをダウンロードして読み取るPhonegap 1.4.1とSenchaを使用してAndroidアプリケーションを作成しています。phonegap メソッド、javascript、または ajax を使用して、ファイルが電話帳に存在するかどうかを確認するにはどうすればよいですか?
14 に答える
私も同じ問題を抱えていました。Darkaico の回答を機能させることはできませんでしたが、Kurt の回答で機能させることができました。
これが私のコードです:
function checkIfFileExists(path){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile(path, { create: false }, fileExists, fileDoesNotExist);
}, getFSFail); //of requestFileSystem
}
function fileExists(fileEntry){
alert("File " + fileEntry.fullPath + " exists!");
}
function fileDoesNotExist(){
alert("file does not exist");
}
function getFSFail(evt) {
console.log(evt.target.error.code);
}
次に、次のように実行するだけです。
checkIfFileExists("path/to/my/file.txt");
上記の答えは私にはうまくいきませんでしたが、これはうまくいきました:
window.resolveLocalFileSystemURL(fullFilePath, success, fail);
.getFile('fileName',{create:false},success,failure)
メソッドを使用してファイルへのハンドルを取得します。コールバックを取得した場合success
、ファイルはそこにあります。それ以外の場合、失敗はファイルに問題があることを意味します。
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);
Darkaico、Kurt、および thomas の回答はうまくいきませんでした。これが私のために働いたものです。
$.ajax({
url:'file///sdcard/myfile.txt',
type:'HEAD',
error: function()
{
//file not exists
alert('file does not exist');
},
success: function()
{
//file exists
alert('the file is here');
}
});
次のコード スニペットをテストしましたが、PhoneGap 3.1 で非常にうまく機能します。
String.prototype.fileExists = function() {
filename = this.trim();
var response = jQuery.ajax({
url: filename,
type: 'HEAD',
async: false
}).status;
return (response != "200") ? false : true;}
if (yourFileFullPath.fileExists())
{}
Kurt と Thomas は、より適切な回答を提供しました。Darkaico の機能は、ファイルが存在するかどうかを確認するだけでなく、ファイルを開いて最後まで読み取る機能を備えているからです。
これは小さなファイルでは問題ありませんが、大きなファイルをチェックするとアプリがクラッシュする可能性があります。
とにかく、.getFile メソッドを使用してください。これが最良のオプションです。
ノート:
ファイルシステムを取得したら、マクロ pg オブジェクトの下の var に保存します。
pg = {fs:{}} // I have a "GOTFS" function... a "fail" function
pg.fs.filesystem = window.requestFileSystem(window.PERSISTENT, 0, pg.fs.GOTFS, pg.fs.fail);
私のコードはとてもシンプルです...
var fileExists = function(path, existsCallback, dontExistsCallback){
pg.fs.fileSystem.root.getFile(path, { create: false }, existsCallback, dontExistsCallback);
// "existsCallback" will get fileEntry as first param
}
現在のすべての回答の問題は、それらがグローバル変数を更新する非同期コールバックに依存していることです。複数のファイルをチェックしている場合、変数が別のコールバックによって設定されている可能性があります。
基本的な Javascript XMLHttpRequest チェックは、Javascript を介してファイルにアクセスできることを同期的にチェックするのに最適です。
function checkFileExists(fileName){
var http = new XMLHttpRequest();
http.open('HEAD', fileName, false);
http.send(null);
return (http.status != 404);
}
ファイルへのフルパスを渡すだけで、次のものを確実に使用できます。
if (checkFileExists(fullPathToFile)) {
// File Exists
} else {
// File Does Not Exist
}
ルートパスを変数に保存するには、次を使用できます。
var fileSystemRoot; // Global variable to hold filesystem root
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsError);
function fsError() {
console.log("Failed to get Filesystem");
}
function fsSuccess(fileSystem) {
console.log("Got Filesystem: Root path " + fileSystem.root);
// save the file to global variable for later access
window.fileSystemRoot = fileSystem.root;
}
var fileexist;
function checkIfFileExists(path){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile(path, { create: false }, fileExists, fileDoesNotExist);
}, getFSFail); //of requestFileSystem
}
function fileExists(fileEntry){
alert("File " + fileEntry.fullPath + " exists!");
fileexist = true;
}
function fileDoesNotExist(){
alert("file does not exist");
fileexist = false;
}
function getFSFail(evt) {
console.log(evt.target.error.code);
fileexist=false;
}
条件を使用できるようになりました
if(fileexist=="true"){
//do something;
}
else if(fileexist=="false"){
//do something else
}
上記の@Thomas Sotiの回答を取り、単純なはい/いいえの応答に切り詰めました。
function fileExists(fileName) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
fileSystem.root.getFile(cordova.file.dataDirectory + fileName, { create: false }, function(){return true}, function(){return false});
}, function(){return false}); //of requestFileSystem
}
// called as
if (fileExists("blah.json")) {
or
var fe = fileExists("blah.json) ;
修正....これは機能しません。私は今修正に取り組んでいます。