devServer.proxy 配置本地代理、测试环境配置nginx解决canvas图片跨域问题

3 篇文章 0 订阅

proxy本地代理访问图片解决跨域问题

devServer.proxy代理

devServer: {
    port: 8001,
    proxy: {
      '/busy': {
        target: 'http://beijing-image.oss-cn-beijing.aliyuncs.com/',
        changeOrigin: true,
        pathRewrite: {
          '^/busy': '/busy'
        }
      }
}

接口访问图片

getImage(url) {
	return axios({ method: 'get', url,responseType: 'blob'})
}
// 接口调用
let url = this.specialImg.split('.com')[1];
getImage(url).then(res => {
	// 将blob转为链接
	const myBlob = new Blob([res.data], { type: "image/jpeg" });
    let src = window.URL.createObjectURL(myBlob)
})

canvas绘制

let image = new Image();
image.crossOrigin = 'anonymous';
image.style = `width: ${w}px;height:${h}px`;
let imageData = {};
image.onload = function() {
  ctx.drawImage(image, 0, 0);   
}
image.src = src;

测试环境接口访问配合nginx配置转发到不同的图片服务器

	nginx通过$arg_url获取接口参数url
	proxy_pass 转到到url

nginx配置

let http = this.specialImg.match(/http[^]+com/)[0]
let url = this.specialImg.split('com/')[1] + '?url=' + http
getImage(url).then(res => {
	// 将blob转为链接
	const myBlob = new Blob([res.data], { type: "image/jpeg" });
    let src = window.URL.createObjectURL(myBlob)
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值