Javascript 根据手机上是否存在文件在jQuery mobile中动态填充listview
这是我代码的开始。它只是从JSON文件填充无序列表。我尝试使用Cordova文件阅读器,但没有任何运气Javascript 根据手机上是否存在文件在jQuery mobile中动态填充listview,javascript,android,jquery,cordova,jquery-mobile,Javascript,Android,Jquery,Cordova,Jquery Mobile,这是我代码的开始。它只是从JSON文件填充无序列表。我尝试使用Cordova文件阅读器,但没有任何运气 document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { alert("Got deviceready"); var reader = new FileReader(); var fileSource = cordova.file.externa
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("Got deviceready");
var reader = new FileReader();
var fileSource = cordova.file.externalDataDirectory;
$.getJSON( "links.json", function( data ) {
$.each( data, function( key, val ) {
var $li = $("<li><a href='#'>"+val.title+"</a></li>");
reader.onloadend = function(evt) {
if(evt.target.result == null) {
$li.find("a").on("click", function(){ downloadPdf(val.title,val.url); });
} else {
$li.find("a").on("click", function(){ openPdf(val.title); });
}
};
// We are going to check if the file exists
reader.readAsDataURL(fileSource + val.title + ".pdf");
$("#linkList").append($li);
$("#linkList").listview('refresh');
});
});
}
如您所见,此示例使用DownloadPdfitle、url函数添加列表项。如果确实存在,我希望列表项调用函数openPdftitle。这些文件保存在cordova.file.externalDataDirectory+title+.pdf中
此代码不会将任何项目添加到我的列表中。您可以使用检查文件是否存在
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);
我尝试过使用FileReader,但没有成功。用我的代码更新了问题。我猜你的文件路径不正确。fileSource+val.title+.pdf我想在文件名之前应该有一个斜杠。请向fileSource+val.title+.pdf发出警报,并检查您得到了什么。还要通过文件管理器检查文件是否确实存在于显示的路径目录中@janlindsoNah,加上一个斜杠,就得到了双斜杠。我现在尝试了一个警报,它返回了正确的路径,如我所见:file:///storage/emulated/0/Android/data/com.helloworld/title.pdfI我已经做了一些研究,我认为存在一些安全问题,不允许应用程序从URL读取文件。