2 回答
TA贡献1826条经验 获得超6个赞
new Vue({
el: '#app',
data () {
return {
src: null
}
},
mounted () {
let self = this
fetch("https://thumbs.dreamstime.com/z/freely-accessible-examination-exposition-mosaic-ar-figures-mosaic-st-petersburg-russia-june-freely-accessible-119320551.jpg")
.then((response) => {
response.blob().then(blobResponse => {
let reader = new FileReader();
reader.readAsDataURL(blobResponse);
reader.onloadend = function() {
let base64data = reader.result;
let img = document.createElement('img')
img.src = base64data
self.$refs['img-container'].appendChild(img)
}
})
})
}
})
img {
height: 300px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div ref="img-container"></div>
</div>
TA贡献1847条经验 获得超7个赞
请求的响应类型(axios请求)应该是 responseType: 'blob'
从服务器接收到数据后,我简单地使用下面这行代码生成一个DOM字符串,然后将其作为图像标签的src:
const imageUrl = window.URL.createObjectURL(new Blob([response.data])) <img :src="imageUrl ">
- 2 回答
- 0 关注
- 106 浏览
添加回答
举报