如何从 react.js 中的图像 url 获取图像类型

How to get the image type from image url in react.js

我正在尝试验证图像 URL 并想检查图像类型。我该怎么做?

假设这张图片 URL :"https://static.wayup.com/company_logo/95tJHraryb_20171220.png"

这些是支持的格式

const SUPPORTED_FORMATS = ["image/jpg", "image/jpeg", "image/png"];

这是我试图对图像进行的验证

image: Yup.mixed()
    .required("Please select Image")
    .test("fileFormat", "Unsupported Format", (value) => {
      How do i get the image type from the URL?
    }),


  [1]: https://static.wayup.com/company_logo/95tJHraryb_20171220.png

只需从 url 中提取图像扩展名。见 :

const SUPPORTED_FORMATS = ["jpg", "jpeg", "png"];

image: Yup.mixed()
    .required("Please select Image")
    .test("fileFormat", "Unsupported Format", (value) => {
      return SUPPORTED_FORMATS.indexOf(get_url_extension(value)) !== -1;
    }),

function get_url_extension( url ) {
    return url.split(/[#?]/)[0].split('.').pop().trim();
}