Get a binary stream image
function getImage (imgUrl: string, defaultImgUrl: string, token: string, el: any, cb: any): void {
const xhr: any = new XMLHttpRequest()
let flag: boolean = false
xhr.open('get', imgUrl, true)
xhr.setRequestHeader('Authorization', token)
xhr.responseType = 'blob'
xhr.onload = function () {
if (this.status === 200) {
if (this.response.type === 'text/xml' || this.response.type === '' || this.response.type === 'image/png' || this.response.type === 'image/jpeg') {
const blob = this.response
const src = window.URL.createObjectURL(blob)
el.src = src
el.onload = () => {
window.URL.revokeObjectURL(src)
}
flag = true
if (cb) {
cb(flag, el)
}
} else {
el.src = defaultImgUrl
flag = false
if (cb) {
cb(flag, el)
}
}
} else {
el.src = defaultImgUrl
flag = false
if (cb) {
cb(flag, el)
}
}
}
xhr.onerror = (err: Error): void => {
console.log('请求失败:', err)
el.src = defaultImgUrl
flag = false
if (cb) {
cb(flag, el)
}
}
xhr.send()
}