【问题标题】:How to get the image type from image url in react.js如何从 react.js 中的图像 url 获取图像类型
【发布时间】:2021-10-24 08:40:42
【问题描述】:

我正在尝试验证图像 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

【问题讨论】:

  • 你能显示你的代码吗?谢谢

标签: javascript reactjs yup


【解决方案1】:

只需从 url 中提取图像扩展名。见https://stackoverflow.com/a/47767860/5771750

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();
}

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多