1

I will explain the code and let's see if someone can help me.

I have this code:

<table>
      <tr>
          <td>
              <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
              <input type="file" name="pathheader" id="pathheader"  class="form-control-file" aria-describedby="fileHelp" style="display:none;">
          </td>
          <td>
              <img src="" id="img2" class="img2" style="width:100%;height:200px;background-color:#ccc;border:2px solid gray;">
              <input type="file" name="pathhome" id="pathhome" class="form-control-file" aria-describedby="fileHelp" style="display:none;"><br>
          </td>
      </tr>
      <tr>
          <td>
              <input type="button" name="" value="Seleccionar header" id="browse_file" class="btn btn-danger form-control">
          </td>
          <td>
              <input type="button" name="" value="Seleccionar home" id="browse_file2" class="btn btn-danger form-control">
          </td>
      </tr>
</table>

And this JS code too:

$("#browse_file").on('click',function(e){
        $("#pathheader").click();
    })
    $("#browse_file2").on('click',function(e){
        $("#pathhome").click();
    })
    $("#pathheader").on('change',function(e){
        var fileInput=this;
        if (fileInput.files[0])
            {
                var reader=new FileReader();
                reader.onload=function(e)
                {
                    $("#img").attr('src',e.target.result);
                }
                reader.readAsDataURL(fileInput.files[0]);
            }
    })
    $("#pathhome").on('change',function(e){
        var fileInput=this;
        if (fileInput.files[0])
            {
                var reader=new FileReader();
                reader.onload=function(e)
                {
                    $("#img2").attr('src',e.target.result);
                }
                reader.readAsDataURL(fileInput.files[0]);
            }
    })

I read other questions and it's possible to make:

onerror="this.style.display='none'"

But I don't want to hide all the style, I want to have the background, border, etc. Only remove the icon img.

Know how to do it with Ajax? If not, it's possible with CSS?

Thanks a lot, any help will be appreciated.

Lluís Puig Ferrer
  • 1,128
  • 7
  • 19
  • 49

2 Answers2

4

You could try injecting a transparent base64-image on error.

onerror="this.src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII='"

Edit after a question from OP:

Base64 is an image format that allows you to "write" an image directly into your HTML file instead of including an external image.

let's look at the image src:

First:

<img src="data:image/png;base64...

This will tell the browser how to display the code that follows. It says that the MIME-Type (in this case, File Type) of the src is .png and that the encoding is base64, which is basically just image data in a String.

After that, the actual Data follows:

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=

These are just letters representing image data like pixels, colors, alpha and so on. If you want to know how exactly it works, read https://www.rfc-editor.org/rfc/rfc3548 or click through the links under Further Reading.

Further Reading

Community
  • 1
  • 1
Tom M
  • 2,815
  • 2
  • 20
  • 47
0

Problem is that you are assigning class and img tag together. so if you are assigning class inside image tag and then referring background image in that class it will show broken image only as it has started referring two image tag so the solution is you use div instead of img tag and see the magic. broken image icon will not appear at all and background image will appear as expected

Steeve
  • 423
  • 2
  • 9
  • 23