Created
April 18, 2017 03:01
-
-
Save oHaiyang/cdbb9f8635e712233c87304db2076eab to your computer and use it in GitHub Desktop.
Convert image to base64 from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static getDataUri (url) { | |
return new Promise((resolve, reject) => { | |
var image = new window.Image() | |
image.crossOrigin = 'Anonymous' | |
image.onload = function () { | |
var canvas = document.createElement('canvas') | |
window.alert('创建了 Canvas') | |
window.alert(canvas) | |
canvas.width = image.width | |
canvas.height = image.height | |
canvas.getContext('2d').drawImage(image, 0, 0) | |
resolve(canvas.toDataURL()) | |
} | |
image.src = url | |
}) | |
} | |
static toDataUrl (url) { | |
return new Promise((resolve, reject) => { | |
var xhr = new XMLHttpRequest() | |
window.alert('xml method') | |
xhr.onload = function () { | |
window.alert('xml method 1') | |
var reader = new FileReader() | |
window.alert('xml method 2') | |
reader.onloadend = function () { | |
resolve(reader.result) | |
} | |
reader.readAsDataURL(xhr.response) | |
} | |
xhr.open('GET', url) | |
xhr.responseType = 'blob' | |
xhr.send() | |
window.alert('xml method 2') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment