Skip to content

Instantly share code, notes, and snippets.

@oHaiyang
Created April 18, 2017 03:01
Show Gist options
  • Save oHaiyang/cdbb9f8635e712233c87304db2076eab to your computer and use it in GitHub Desktop.
Save oHaiyang/cdbb9f8635e712233c87304db2076eab to your computer and use it in GitHub Desktop.
Convert image to base64 from URL
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