How do I check if file exists in jQuery or pure JavaScript?

前端 未结 18 2072
闹比i
闹比i 2020-11-22 00:56

How do I check if a file on my server exists in jQuery or pure JavaScript?

相关标签:
18条回答
  • 2020-11-22 01:37

    I wanted a function that would return a boolean, I encountered problems related to closure and asynchronicity. I solved this way:

    checkFileExistence= function (file){
        result=false;
        jQuery.ajaxSetup({async:false});
        $.get(file)
            .done(function() {
               result=true;
            })
            .fail(function() {
               result=false;
            })
        jQuery.ajaxSetup({async:true});
        return(result);
    },
    
    0 讨论(0)
  • 2020-11-22 01:39

    I was getting a cross domain permissions issue when trying to run the answer to this question so I went with:

    function UrlExists(url) {
    $('<img src="'+ url +'">').load(function() {
        return true;
    }).bind('error', function() {
        return false;
    });
    }
    

    It seems to work great, hope this helps someone!

    0 讨论(0)
  • 2020-11-22 01:39

    This doesn't address the OP's question, but for anyone who is returning results from a database: here's a simple method I used.

    If the user didn't upload an avatar the avatar field would be NULL, so I'd insert a default avatar image from the img directory.

    function getAvatar(avatar) {
        if(avatar == null) {
            return '/img/avatar.jpg';
        } else {
            return '/avi/' + avatar;
        }
    }
    

    then

    <img src="' + getAvatar(data.user.avatar) + '" alt="">
    
    0 讨论(0)
  • 2020-11-22 01:39

    It works for me, use iframe to ignore browsers show GET error message

     var imgFrame = $('<iframe><img src="' + path + '" /></iframe>');
     if ($(imgFrame).find('img').attr('width') > 0) {
         // do something
     } else {
         // do something
     }
    
    0 讨论(0)
  • 2020-11-22 01:40

    i used this script to add alternative image

    function imgError()
    {
    alert('The image could not be loaded.');
    }
    

    HTML:

    <img src="image.gif" onerror="imgError()" />
    

    http://wap.w3schools.com/jsref/event_onerror.asp

    0 讨论(0)
  • 2020-11-22 01:40

    This is an adaptation to the accepted answer, but I couldn't get what I needed from the answer, and had to test this worked as it was a hunch, so i'm putting my solution up here.

    We needed to verify a local file existed, and only allow the file (a PDF) to open if it existed. If you omit the URL of the website, the browser will automatically determine the host name - making it work in localhost and on the server:

    $.ajax({
    
        url: 'YourFolderOnWebsite/' + SomeDynamicVariable + '.pdf',
        type: 'HEAD',
        error: function () {
            //file not exists
            alert('PDF does not exist');
    
        },
        success: function () {
            //file exists
            window.open('YourFolderOnWebsite/' + SomeDynamicVariable + '.pdf', "_blank", "fullscreen=yes");
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题