通过 fetch Head 获取web链接文件的 mimeTypeedit icon

作者:
lynn-ssk
Fork(复制)
下载
嵌入
设置
BUG反馈
index.html
现在支持上传本地图片了!
            
            <!DOCTYPE html>
<html lang="en">

<body>
  <script>
    async function getUrlMimeTypeWithFetchHead(url) {
      try {
        // 发送 HEAD 请求,仅获取响应头
        const response = await fetch(url, {
          method: 'HEAD'
        });
        // 从响应头中获取 Content-Type
        const contentType = response.headers.get('Content-Type');
        console.log('Content-Type:', contentType);
        // 格式可能如下 "application/pdf; qs=0.001" 取分号前面的部分 就是 mimeType
        return contentType?.split(';')[0]?.trim() || null
      } catch (error) {
        console.error('Error fetching Content-Type:', error);
        return null;
      }
    }
    getUrlMimeTypeWithFetchHead('https://cdn.openai.com/business-guides-and-resources/a-practical-guide-to-building-agents.pdf');
  </script>
</body>

</html>
        
预览
控制台
清空