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

前端 未结 18 2110
闹比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: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

    
    

提交回复
热议问题