let bgcImage = 'http://192.168.0.83:9080/files/4a9c3056-9b9b-4b41-b8e2-fd9f27023c41.jpg' let image = new Image() image.crossOrigin = '' // 必须有这个 image.src = bgcImage image.onload = () => { // 图片加载完成后,调用getBase64Image方法 let base64ImageSrc = getBase64Image(image) console.log(base64ImageSrc ) } export function getBase64Image(image, width, height) { // width、height调用时传入具体像素值,控制大小 ,不传则默认图像大小 let canvas = document.createElement('canvas') canvas.width = width !== undefined ? width : image.width canvas.height = height !== undefined ? height : image.height let ctx = canvas.getContext('2d') ctx.drawImage(image, 0, 0, canvas.width, canvas.height) let ext = image.src.substring(image.src.lastIndexOf('.') + 1).toLowerCase() let dataURL = canvas.toDataURL('image/' + ext) return dataURL }