使用Blob本地预览图片(vue环境)

本文介绍了如何使用JavaScript从接口获取图片URL并转化为本地对象URL,展示了在Vue组件中利用axios下载文件并设置img标签src的过程。重点在于代码实现和图片资源的处理。

话不多说,上代码

<template>
    <div>
        <img
           :src="imgSrc"
           alt=""
        >
    </div>
</template>
<script>
export default {
    data(){
        return {
            imgSrc: '',
        }
    },
    created() {
        getImg(this.$route.params.id).then(res => {
            const blob = new Blob([res],{type:'image/png'})
            const url = windows.URL.creatObjectURL(blob)
            this.imgSrc = url
        }
    }
}
</script>

 this.imgSrc 数据格式:

["bobl:http//localhose:8080/******"]

接口设置:

 export function downFile(url) {
    return request({
        url:url,
        method: 'get',
        responseType:'blob'
    })
}
 expost const getImg = (id) => downFile(`地址${id}`)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值