当前位置:网站首页>请求二进制数据和base64格式数据的预览显示
请求二进制数据和base64格式数据的预览显示
2022-07-05 22:19:00 【ws_code_csdn】
1、请求二进制数据,各种格式的数据预览显示。
axios({
method: 'get',
url: `getFile/${type}`,
headers: {
Authorization: userStore.token,
},
// 这里是必须的 'blob'
responseType: 'blob',
}).then((res: any) => {
// pdf 格式
if (type == 1) {
let blob = new Blob([res.data], { type: 'application/pdf' });
pdfSrc.value = URL.createObjectURL(blob);
window.open(pdfSrc.value);
} else if (type == 2) {
// 图片 jpeg格式
let blob = new Blob([res.data], { type: 'image/jpeg' });
imgSrc.value = window.URL.createObjectURL(blob);
} else if (type == 3) {
// 音频
let blob = new Blob([res.data], { type: 'wav/audio' });
if (audio.value) {
audio.value.src = window.URL.createObjectURL(blob);
audio.value.play();
}
} else if (type == 4) {
// 图片 excel格式
const link = document.createElement('a');
let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });
link.style.display = 'none';
link.href = URL.createObjectURL(blob); //创建一个指向该参数对象的URL
link.setAttribute('download', `表资源${new Date().getTime()}.xls`);
document.body.appendChild(link);
link.click(); //触发下载
document.body.removeChild(link);
}
});
注意点:
A、要把 responseType 设置为 ‘blob’;
B、要把对应的type设置好,比如图片 new Blob([res.data], { type: 'image/jpeg' })。
2、base64格式数据图片预览显示
getOperImg()
.then((res) => {
if (res) {
const { code, data } = res;
if (code == 200) {
if (Array.isArray(data) && data.length != 0) {
const { operPic } = data[0];
// operPic 就是请求来的base64图片数据
// 最终的图片地址 需要前边拼接 data:image/jpg;base64,
imgSrc.value = `data:image/jpg;base64,${operPic}`;
}
}
}
})
.catch((err) => {
console.log('获取图片', err);
});
边栏推荐
- Nacos installation and service registration
- Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
- Blocking of concurrency control
- Interprocess communication in the "Chris Richardson microservice series" microservice architecture
- 如何开发引入小程序插件
- A substring with a length of three and different characters in the leetcode simple question
- Leetcode simple question ring and rod
- Sentinel production environment practice (I)
- Cobaltstrike builds an intranet tunnel
- Bitbucket installation configuration
猜你喜欢
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
Leetcode simple question: check whether each row and column contain all integers
Oracle advanced query
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
K210 learning notes (IV) k210 runs multiple models at the same time
Pl/sql basic syntax
Oracle triggers
Alternating merging strings of leetcode simple questions
Technology cloud report: how many hurdles does the computing power network need to cross?
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
随机推荐
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
Common interview questions of JVM manufacturers
Hcip day 16
AD637使用筆記
[Yugong series] go teaching course in July 2022 004 go code Notes
Granularity of blocking of concurrency control
Leetcode simple question: the minimum cost of buying candy at a discount
Postman核心功能解析-参数化和测试报告
Server optimization of performance tuning methodology
如何開發引入小程序插件
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Blocking of concurrency control
Meituan dynamic thread pool practice ideas, open source
Web3为互联网带来了哪些改变?
The difference between MVVM and MVC
如何创建线程
boundary IoU 的计算方式
从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
Concurrency control of performance tuning methodology
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~