当前位置:网站首页>Export file or download file
Export file or download file
2022-07-19 07:48:00 【guizi0809】
Use here axios Request interface , The same is true for other tools
axios({
url: 'xxxxx'
methods: 'get', // get Request to get the file stream
params: {
},
responseType: 'blob', // Note that the response type must be specified here , Otherwise, the interface returns garbled
})
Successfully called the interface , Interface to return blob Binary object of type 
export file or Download the file
const res = await xxxApi(); // request api get data ,res by Blob object
const blobUrl = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.style.display = 'none';
a.download = 'XXX File name ';
a.href = blobUrl;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(blobUrl); // As long as the mapping exists ,Blob We can't recycle , So once you no longer need to quote , Must be revoked URL, release blob object perhaps Use blobUrl = null
Be careful : If set responseType by arraybuffer, Then the data returned by the calling interface is ArrayBuffer Primitive binary type

Using the above method directly will report an error

Here we need to ArrayBuffer Type into Blob type
const res = await xxxApi(); // request api get data ,res by ArrayBuffer object
const blob = new Blob([res], {
type: "application/vnd.ms-excel" }); // Be careful !!! Here we need to ArrayBuffer Type into Blob type , You need to specify the type file type , Otherwise default to txt type
const blobUrl = window.URL.createObjectURL(res);
let a = document.createElement('a');
a.style.display = 'none';
a.download = 'XXX File name ';
a.href = blobUrl;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(blobUrl); // As long as the mapping exists ,Blob We can't recycle , So once you no longer need to quote , Must be revoked URL, release blob object perhaps Use blobUrl = null
Recommended direct use blob, Because use arrayBuffer You need to turn to Blob type , And you need to specify the file type , Without knowing the file type , His tacit view is that txt, It's easy to make mistakes .
The end result is as follows :

边栏推荐
- Environment variables and folder placement location
- Spark3.x源码编译
- Freebsd12 install gnome3 graphical interface
- FreeBSD 12 installing RPM packages
- This article introduces you to SOA interface testing
- 利用PCA来简化数据
- Product Case Interviews
- 修改select样式
- High concurrency day04 (Zab protocol, observer, NC, Avro, RPC)
- Promote trust in the digital world
猜你喜欢

修改checkbox样式

【MySQL】 锁机制:InnoDB引擎中锁分类以及表锁、行锁、页锁详解

Introduction to arm development environment

FMC sub card: 8-channel 125msps sampling rate 16 bit AD acquisition sub card

FMC sub card: 4-channel 250msps sampling rate 16 bit AD acquisition sub card

@How can conditionalonmissingbean cover beans in third-party components

60. Clear cache

The best storage scheme of MCU CS Chuangshi SD NAND

面部关键点检测-CNN

Flink入门到实战-阶段二(图解运行时架构)
随机推荐
自用的一套Jenkins样式
Coursera deep learning notes
TSN security protocol (802.1qci)
机器学习之随机森林
卷积神经网络CNN
六十、清除缓存
nodejs
Understanding of v2x test series v2x phase II application scenario
2022危险化学品经营单位主要负责人复习题及模拟考试
Jenkins 忘记密码怎么办?
力扣114题:二叉树展开链表
修改select样式
FreeBSD 12 changing the background of the startup interface
4-channel FMC interface baseband signal processing board (2 FMC interfaces, 2 fmc+ interfaces)
【MySQL】 事务:事务基础知识、MySQL事务实现原理、事务日志 redolog & undolog 详解
Pytorch随记(2)
Flink introduction to practice - phase II (illustrated runtime architecture)
【MySQL】 锁机制:InnoDB引擎中锁分类以及表锁、行锁、页锁详解
Flink entry to actual combat - phase III (datastream API)
Spark3.x源码编译