简体   繁体   English

Javascript:如何在没有指定扩展名的情况下从文件夹检查图像是否存在?

[英]Javascript : how to check image exist or not from folder without specify extension?

I am working in web application(bookshop) using php, Used Ajax to filter books, but updating image gave big issue from ajax, because i used id as image name, so i have only image name without extension, now i have to update image in book list, I have an image name but i do not know the extension of the image, but i have to check the image is exist or not from the folder without extension, if exist i have to get full image name with extension, else i have to go for default image. 我正在使用php在Web应用程序(书店)中工作,使用Ajax来过滤书籍,但是更新图像给ajax带来了很大的问题,因为我使用id作为图像名称,所以我只有图像名称而没有扩展名,现在我必须更新图像在书单中,我有一个图像名称,但我不知道该图像的扩展名,但是我必须从无扩展名的文件夹中检查该图像是否存在,如果存在,则必须获取具有扩展名的完整图像名,否则我必须去默认图像。

Referred many answers but they used extension with url. 引荐了许多答案,但他们使用带有url的扩展名。

Sample Ajax code: 样本Ajax代码:

$.ajax({
        type: 'POST',
        dataType: 'json',
        url: '<?php echo base_url(); ?>bookShopC/filterbooks', 
        data: {bookname:bookname},
        success: function(data)
        {       
            $('#book_container_fil').html('');
            $.each(data, function(index,element){
                var book_name = element[0].book_id; // here i have book name without extension
                var full_file_name = checkFileExist(book_name); // tried with some functions but not works without ext.
                $('#book_container_fil').append("<div class='book_box'>
                <div class='book_img'>
                    // here i have to update if cover photo exist else print default photo
                    <img src='http://sample.com/assets/images/book-covers/default.png'>                 
                </div>
                <div class='book_data'><div></div>");
            });

        }
    });

Function to check if file exist: 检查文件是否存在的功能:

function checkFileExist(book_name)
{   
    var picurl = 'http://sample.com/assets/images/cover_photo/'+book_name;
    var img = new Image();
    img.src = picurl;
    var re = img.height != 0;
    alert(re);
}

You could use something like this: 您可以使用如下形式:

function checkFileExist(book_name)
{   

    var picurl = 'http://sample.com/assets/images/cover_photo/';
    var img1 = new Image(); 
    img1.src = picurl+picname+'.png';
    var img2 = new Image(); 
    img2.src = picurl+picname+'.jpg';
    var img3 = new Image(); 
    img3.src = picurl+picname+'.gif';
    if(img1.height != 0)
    {
       return picname+'.png';
    }
    else if(img2.height != 0)
    {
       return picname+'.jpg';
    }
    else if(img3.height != 0)
    {
       return picname+'.gif';
    }
    else
    {
       return 'default_cover.png';
    }
}

It's hard to validate a file path from a javascript. 很难从javascript验证文件路径。 you could get image name via php. 您可以通过php获取图像名称。 I believe you use codeigniter. 我相信您使用codeigniter。 so crete an another function near filterbooks call 'getFullFileName' 所以在过滤器附近创建另一个函数调用'getFullFileName'

function getFullFileName(){

   $bookname = $_POST['bookname'];
   $result = glob ("./assets/images/book-covers/$bookname.*");

   if(isset($result[0]))
     echo $result[0];
   else
     echo "http://sample.com/assets/images/book-covers/default.png";


}

so in the ajax part: 所以在ajax部分:

 $.ajax({
  type: 'POST',
  dataType: 'json',
  url: '<?php echo base_url(); ?>bookShopC/filterbooks',
  data: {
    bookname: bookname
  },
  success: function(data) {
    $('#book_container_fil').html('');
    $.each(data, function(index, element) {

       var book_name = element[0].book_id;

      $.ajax({
        type: 'POST',
        url: '<?php echo base_url(); ?>bookShopC/getFullFileName',
        data: {
          bookname: book_name
        },
        success: function(imgurl) {

          $('#book_container_fil').append(imgurl);
        }
      });
    });
  }
});

If you give me the how look like 'data' json I can give you a better answer without using two ajax. 如果您给我“数据” json的外观,我可以在不使用两个ajax的情况下为您提供更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM