通过Javascript检测图像的mime类型。

时间:2022-02-23 21:23:28

i am detecting images on my webpage with javascript document.images function which returns an rray of images. Is there any way to get the mime type of the image from this since i am not using any extension names on images which are loading?

我正在用javascript文档检测网页上的图像。图像函数,返回图像的rray。由于我没有在正在加载的图像上使用任何扩展名,有什么方法可以从这里获得图像的mime类型吗?

2 个解决方案

#1


4  

I don't think this is possible, apart from requesting the image again in jQuery and trying to use a MIME type check on the data. (You might be able to send a HEAD request and get whatever Content-type was returned)

我认为这是不可能的,除了再次用jQuery请求图像并尝试对数据使用MIME类型检查之外。(您可以发送HEAD请求并获取返回的内容类型)

#2


0  

According with other answers, I got pretty surprised to find out it was apparently not possible without requesting the image again. (??? That seems weird to me but oh well…)

根据其他的答案,我很惊讶地发现,如果不再次请求图像,这显然是不可能的。(? ? ?这在我看来似乎很奇怪,但是……

Another hacky-but-might-be-enough-for-your-use-case-solution that comes in my mind would be to:

我脑海中出现的另一种“用黑客的方式”的用例解决方案是:

Parse the img.src in an attempt of guessing the file type.

解析img。尝试猜测文件类型的src。

  • "Guessing?" you may ask…
  • “猜吗?”你可能会问……
  • Yes because the extension might not be included in the src or could be misleading.
  • 是的,因为扩展可能不包含在src中,或者可能具有误导性。

#1


4  

I don't think this is possible, apart from requesting the image again in jQuery and trying to use a MIME type check on the data. (You might be able to send a HEAD request and get whatever Content-type was returned)

我认为这是不可能的,除了再次用jQuery请求图像并尝试对数据使用MIME类型检查之外。(您可以发送HEAD请求并获取返回的内容类型)

#2


0  

According with other answers, I got pretty surprised to find out it was apparently not possible without requesting the image again. (??? That seems weird to me but oh well…)

根据其他的答案,我很惊讶地发现,如果不再次请求图像,这显然是不可能的。(? ? ?这在我看来似乎很奇怪,但是……

Another hacky-but-might-be-enough-for-your-use-case-solution that comes in my mind would be to:

我脑海中出现的另一种“用黑客的方式”的用例解决方案是:

Parse the img.src in an attempt of guessing the file type.

解析img。尝试猜测文件类型的src。

  • "Guessing?" you may ask…
  • “猜吗?”你可能会问……
  • Yes because the extension might not be included in the src or could be misleading.
  • 是的,因为扩展可能不包含在src中,或者可能具有误导性。