Check if file exist using script.
1)same domain this should work:
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
or
function fileExists(url) {
if(url){
var req = new XMLHttpRequest();
req.open('GET', url, false);
req.send();
return req.status==200;
} else {
return false;
}
}
2)
function ImageExist(url)
{
var img = new Image();
img.src = url;
return img.height != 0;
}
3)
<img src="image.gif" onerror="imgError()" />
4)
$.get(url)
.done(function() {
// exists code
}).fail(function() {
// not exists code
})
5)
$.ajax({
url:'http://www.example.com/somefile.ext',
type:'HEAD',
error: function()
{
//file not exists
},
success: function()
{
//file exists
}
});
For a client computer this can be achieved by:try { var myObject, f; myObject = new ActiveXObject("Scripting.FileSystemObject"); f = myObject.GetFile("C:\\img.txt"); f.Move("E:\\jarvis\\Images\\"); } catch(err) { alert("file does not exist") }
6)Image exists.
function checkImage(src) {
var img = new Image();
img.onload = function() {
// code to set the src on success
$('#image-test').css('background', 'url(' + src + ') no-repeat 50% 50%');
};
img.onerror = function() {
// doesn't exist or error loading
alert('no image');
};
img.src = src; // fires off loading of image
}
checkImage('http://www.google.com/images/logo_sm.gif');
No comments:
Post a Comment