0

I am working in IE10 for a page. In that am using some images. few images were contains false image type(original image type is "png" but it has extension as "jpg"). The false images were broken in IE10(but shows in firefox). How to find the original image type using javascript avoiding the image extension and show in IE10?

need code like

   var imgEle = document.createElement('img');
   imgEle.src = "imagePath/imageName."+findImageType("imagePath/imageName.jpg");

    function findImageType(imagUrl)
    {
      //do stuff
      return imageType;
    }

is it Possible? if not what are the ways to find original image type?

Gowsikan
  • 5,571
  • 8
  • 33
  • 43

2 Answers2

1

I think you can use an HTTP HEAD request for that (through XMLHttpRequest).

You can get the mime-type of your image with that method, so you should be able to check his true type.

TiPi
  • 330
  • 4
  • 19
0

Using a combination of deokman's answer and adam's from this SO question:

HTTP HEAD Request in Javascript/Ajax?

You could build a function that makes a specific HEAD request to the server that has the image, and then grab out the content type using getResponseHeader("content-type"), you could then translate the different content type strings to actual image extensions. If the server you are requesting the images from correctly calculates the content-type (i.e. not based on extension), then this would be one way.

The other way would be to create a server-side script that actualy analyses the headers of each file (reading the first few bytes of binary data) to determin the image type, and call this again via ajax.

The problem you would get with either method however is you'd have to change the extension before you could display the image, and this would then point to a file that doesn't exist. So the best way to fix the problem is to rename the files with the correct extension in the first place.

Community
  • 1
  • 1
Pebbl
  • 34,937
  • 6
  • 62
  • 64