// base64 转blob
export function dataURLtoBlob(dataurl) {
var bstr = atob(dataurl)
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new Blob([u8arr], { type: 'image/jpg' })
}
// base64 转文件
export function dataURLtoFile(dataurl, filename) { // 将base64转换为文件
var bstr = atob(dataurl)
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], filename, {
type: 'image/jpg'
})
}
// 获取图片转base64
getBase64(file) {
return new Promise(function(resolve, reject) {
const reader = new FileReader()
let imgResult = ''
reader.readAsDataURL(file)
reader.onload = function() {
imgResult = reader.result
}
reader.onerror = function(error) {
reject(error)
}
reader.onloadend = function() {
resolve(imgResult)
}
})
},