当前位置:网站首页>请求二进制数据和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);
});
边栏推荐
- 344. Reverse String. Sol
- Pl/sql basic syntax
- Leetcode simple question check whether all characters appear the same number of times
- Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
- Leetcode simple question: check whether each row and column contain all integers
- ESP32 hosted
- The difference between MVVM and MVC
- 如何向mongoDB中添加新的字段附代码(全)
- Database tuning solution
- Stored procedures and stored functions
猜你喜欢
2022软件测试工程师涨薪攻略,3年如何达到30K
boundary IoU 的计算方式
K210 learning notes (IV) k210 runs multiple models at the same time
Metaverse Ape获Negentropy Capital种子轮融资350万美元
科技云报道:算力网络,还需跨越几道坎?
Practice: fabric user certificate revocation operation process
装饰器学习01
Database recovery strategy
Blocking protocol for concurrency control
航海日答题小程序之航海知识竞赛初赛
随机推荐
如何创建线程
Win11运行cmd提示“请求的操作需要提升”的解决方法
Oracle advanced query
Index optimization of performance tuning methodology
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Web3为互联网带来了哪些改变?
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
Official clarification statement of Jihu company
Business learning of mall order module
装饰器学习01
How to view Apache log4j 2 remote code execution vulnerability?
Pinctrl subsystem and GPIO subsystem
ESP32 hosted
The difference between MVVM and MVC
Overview of database recovery
Depth first DFS and breadth first BFS -- traversing adjacency tables
Pl/sql basic syntax
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
The simple problem of leetcode is to split a string into several groups of length K
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial