图片通过URL下载,停用缓存报CROS错误

1. 现象:

2. 说明:

应用第三方华为Obs存储系统,前端希望通过url下载该图片,停用缓存后,发现下载时一直提示CROS错误

3. 原理(暂不明)

估计是因为停用缓存以后不同域导致的?


考虑图片是否被浏览器缓存住了。

4. 解决方案

在图片url后面添加一个时间戳 const imageUrl = `${data.picture}?t=${new Date().getTime()}` 

 const imgToBase64 = async (data: any) => {
    const imageUrl = `${data.picture}?t=${new Date().getTime()}` // 添加时间戳
    fetch(imageUrl)
      .then(response => response.blob())
      .then(blob => {
        // 使用FileReader将图片数据转换为Base64
        const reader: any = new FileReader()
        reader.onload = () => {
          dealPicData({
            picture: reader.result,
            related: data.related,
          })
        }
        reader.readAsDataURL(blob)
      })
      .catch(error => {
        console.error('获取图片失败:', error)
      })
  }

猜你喜欢

转载自blog.csdn.net/qq_41885295/article/details/132878796