后端返回Bolb对象

 

 

 

 

 

 

Blob 对象表示一个不可变、原始数据的类文件对象。它的数据可以按文本或二进制的格式进行读取,也可以转换成 ReadableStream 来用于数据操作。

 

前端展示原始文件有两种方式:

1、将接口直接写到 src 里面(比如 img 的src) ----> 不方便拿接口头字段信息。

2、转换为URL对象。

axios.get("/common/genCaptcha?"+new Date().getTime(),{responseType:'blob'}).then(res=>{
                console.log(res,"0-0-0-");
                if(res && res.status === 200){
                    sessionStorage.setItem("captcha",res.headers.captcha);

                    let img = document.createElement('img');
                    img.src = window.URL.createObjectURL(res.data); //参数为一个指定的file对象或Blob对象
                                                                    //返回的是一个URL对象
                    img.onload = ()=>{
                        window.URL.revokeObjectURL(this.src) //img加载完后 释放img.src所指向的URL对象
                        this.setState({
                            boldimg:img.src
                        })
                        img = null
                    }
                }
        })

 官方Bolb操作方法:  https://developer.mozilla.org/zh-CN/docs/Web/API/Blob

posted @ 2022-03-18 10:26  Action_swt  阅读(780)  评论(0编辑  收藏  举报