当前位置:网站首页>请求二进制数据和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);
});边栏推荐
- Overview of concurrency control
- [groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
- Some tutorials install the database on ubantu so as not to occupy computer memory?
- Nacos installation and service registration
- 如何向mongoDB中添加新的字段附代码(全)
- Summary of concurrency control
- Draw a red lantern with MATLAB
- 如何快速体验OneOS
- 2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
- Two stage locking protocol for concurrency control
猜你喜欢

Qtquick3d real time reflection

Recovery technology with checkpoints

Installation of VMware Workstation

Solutions for unexplained downtime of MySQL services

What about data leakage? " Watson k'7 moves to eliminate security threats

Meituan dynamic thread pool practice ideas, open source

Technology cloud report: how many hurdles does the computing power network need to cross?

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程

The real situation of programmers

2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
随机推荐
Two stage locking protocol for concurrency control
2022软件测试工程师涨薪攻略,3年如何达到30K
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
航海日答题小程序之航海知识竞赛初赛
Server optimization of performance tuning methodology
AD637使用筆記
Pl/sql basic case
Sub total of Pico development
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
How can Bluetooth in notebook computer be used to connect headphones
Promql demo service
Depth first DFS and breadth first BFS -- traversing adjacency tables
The difference between MVVM and MVC
Common interview questions of JVM manufacturers
Leetcode simple question ring and rod
如何向mongoDB中添加新的字段附代码(全)
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
Bitbucket installation configuration
点到直线的距离直线的交点及夹角
Solutions for unexplained downtime of MySQL services