I'm trying to write the simple part of code in pure java script that should just check if the file exists in some specific folder. I lost a day already trying to get it work, I've seen many similar threads, but none of them worked for me.
I'm trying to run the simple html page on my PC on the win7.
I've tried the code from this thread javascript check if file exists
function fileExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
But I'm getting error in http.send()
line "A network error occurred."
Also this thread doesn't help: Can't check if file on sdcard exists
String path = Environment.getExternalStorageDirectory().getPath() + "/ping.xml";
File f = new File(path);
if (f.exists()) {
// do your stuff
}
else {
// do your stuff
}
The line with new File(path)
gives an error: "File requires at least 2 arguments, but only 1 were passed"
Well i found out the correct syntax for it, it should look something like:
File f = new File([""], path);
It's really annoying that i found on this forum plenty times the same incorrect syntax for new File
with only 1 parameter instead of 2 of them, but ok. Even when i fixed this in that way it doesn't work - the method
f.exists()
always returns the false
This is the current code that i have now:
function MyFunc()
{
var myfile = new File([""], 'F:\\abc.txt');
if (!myfile.exists) {
alert(myfile.name + " could not be found!");
}
else
{
alert("file exists!");
}
}
I tried to see what is inside the myfile array with such methods:
myfile.name
myfile.size
myfile.webkitRelativePath
but seems that only first of them works (the name of the file). Other return null data. So maybe the reason is that something wrong in line var myfile = new...