Binaryify / vue-qr

The Vue Component for Awesome-qr.js
MIT License
849 stars 123 forks source link

加载图片为网络上的图片,现在有办法解决跨域吗。 #94

Open yh17838 opened 3 years ago

yh17838 commented 3 years ago

加载不同域名下的图片,想问问有办法解决跨域请求吗。 谢谢。

Binaryify commented 2 years ago

跨域不是客户端能处理的,也要服务端支持访问的

AsherSun commented 2 years ago

可以通过请求拿到图片的二进制数据,然后写入到Img标签中。canvas中加载img标签。如果图片请求就是跨域的,那么需要服务端支持了

window.fetch(url).then(response => {
 respose.blob().then((result) => {
 const url = URL.createObjectURL(result);
 const image = new Image();
 image.crossOrigin = 'Anonymous'
 image.onload = () => {
  resolve({ width: image.width, height: image.height, src: url, image, });
 }
 image.onerror = reject;
 image.src = url;
 }).catch(() => null)
}).catch(() => null)

加载不同域名下的图片,想问问有办法解决跨域请求吗。 谢谢。