export function downloadBlob(
data,
name,
type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
) {
if (name) {
if (name.lastIndexOf(".") < 0) {
name += ".xlsx";
}
} else {
name += ".xlsx";
}
let blob = new Blob([data], { type });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, name);
} else {
let objectUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
a.download = name;
a.href = objectUrl;
a.click();
document.body.appendChild(a);
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", false, false);
a.dispatchEvent(evt);
document.body.removeChild(a);
}
}
/**
* 导出文件下载
* @param {[type]} res 后台返回response
* @return {[type]} [description]
*/
export function exportDownload(that,res){
if (res.data.type == 'application/json') {
// 将blob文件流转换成json
const reader = new FileReader();
reader.readAsText(res.data);
reader.onload = function (event) {
const message = JSON.parse(event.target.result).message;
that.$message.error(message);
}
return false;
}
const blob = new Blob([res.data]);
let str = res.headers['content-disposition'];
let filename = decodeURI(str.substr(str.indexOf('=') + 1));
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
//ie使用的下载方式
window.navigator.msSaveOrOpenBlob(blob, filename);
} else {
let elink = document.createElement("a");
// 设置下载文件名
elink.download = filename;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
}
}
// 下载xls
export function downLoadFile (data, name) {
let a = document.createElement('a')
a.download = `${name}.xls`;
a.style.display = 'none'
//获取请求返回的response对象中的blob 设置文件类型,这里以excel为例
let blob = new Blob([data], { type: 'application/[vnd.ms](http://vnd.ms/)-excel' })
//创建一个临时的url指向blob对象,创建url之后可以模拟对此文件对象的一系列操作,例如:预览、下载
a.href = URL.createObjectURL(blob)
document.body.appendChild(a);
a.click()
document.body.removeChild(a);
}
/**
* 下载流文件文件工厂函数
* @param {*} param0
* @returns
*/
export function axiosDownloadFactory({ url, params }) {
return new Promise((resolve, reject) => {
axios({
method: 'get',
url,
params,
responseType: 'blob'
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
const fileName = response.headers['content-disposition'].match(/=(.*)$/)[1]; // 此方法为获取后台文件名,也可以当作参数传递进来
link.href = url;
link.setAttribute('download', decodeURI(fileName));
document.body.appendChild(link);
link.click();
resolve();
}).catch((error) => {
reject(error)
})
})
}
blob文件下载记录
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题描述 如何查看Storage Account的删除记录,有没有接口可以下载近1天删除的Blob文件信息呢?因为...
- 1. 使用FileSystemResource,以文件系统的绝对路径的方式访问静态资源 FileSystemRes...
- 工作以来,一直在用django框架写接口,对于django的前端模板文件接触不多,最近在学习使用django快速搭...
- 一. SpringBoot(返回cookies信息的get接口开发) 1. 新建main方法 在java文件夹下,...