已解决
Vue图片URL转File实践[已解决跨域问题]
来自网友在路上 11188118提问 提问时间:2023-11-21 20:21:16阅读次数: 118
最佳答案 问答题库1188位专家为你答疑解惑
async imgToFile(url) {const self = thisconst image = new Image()//let url = "https://shenjianblog.oss-cn-shanghai.aliyuncs.com/pic/20220612/sfxs.jpg"// 使用 fetch 获取图像作为 Blob 对象const response = await fetch(url.replace("https://shenjianblog.oss-cn-shanghai.aliyuncs.com", "/imgApi"));const blob = await response.blob();const imgSrc = URL.createObjectURL(blob)// 加载 Blob 对象的 URLimage.src = imgSrc;image.onload = () => {const canvas = document.createElement('canvas')canvas.width = image.widthcanvas.height = image.heightconst context = canvas.getContext('2d')context.drawImage(image, 0, 0, image.width, image.height);canvas.toBlob(function (blob) {const selectedFile = new File([blob], 'image.png', {type: 'image/png'});console.log(selectedFile)})};},
在Vue.config.js中配置代理即可
// 配置代理devServer:{open: true,proxy:{"/imgApi": {target: 'https://shenjianblog.oss-cn-shanghai.aliyuncs.com',changeOrigin: true,pathRewrite:{'^/imgApi':'' //请求的时候使用这个api就可以}}}}
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"Vue图片URL转File实践[已解决跨域问题]":http://eshow365.cn/6-41530-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!