javascript使用Blob对象实现的下载文件操作示例
作者:harmsworth2016 时间:2024-04-29 13:14:52
本文实例讲述了javascript使用Blob对象实现的下载文件操作。分享给大家供大家参考,具体如下:
Blob对象
前言
环境
操作
总结
Blob是一个类文件的不可变的原始数据对象,非javascript原生数据类型,File对象就是继承自Blob对象,且在Blob的基础上进行扩展,以便支持用户系统上的文件。
前言
最近在做以post请求方式导出excel时,想到了可以使用Blob对象将后台返回的输出流以arraybuffer或blob的格式接收交给Blob处理,最后使用URL生成链接,供浏览器下载excel。
环境
vue2.x
webpack3.x
axios
操作
import axios from 'axios'
/**
* 从服务器下载excel
*/
function downloadExcel (settings) {
const defaultOptions = {
responseType: 'arraybuffer'
}
Object.assign(settings.options, defaultOptions)
requestToResponse(settings).then(res => {
const filename = decodeURI(res.headers['content-disposition'].split('filename=')[1])
if ('download' in document.createElement('a')) {
downloadFile(res.data, filename)
} else {
Message.error('浏览器不支持')
}
})
}
/**
* 发送http请求
* @param {Object} settings api参数
* @returns reponse
*/
function requestToResponse (settings) {
const defaultParams = {
timeout: 45000,
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/json'
},
responseType: 'json',
method: 'POST'
}
Object.assign(defaultParams, settings)
return new Promise((resolve, reject) => {
axios(defaultParams).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
/**
* blob下载(兼容IE)
* @param {String} content 文件内容
* @param {String} filename 文件名
*/
function downloadFile (content, filename) {
const blob = new Blob([content])
// IE
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, filename)
} else {
imatateDownloadByA(window.URL.createObjectURL(blob), filename)
}
}
/**
* 通过a标签模拟下载
* @param {String} href
* @param {String} filename
*/
function imatateDownloadByA (href, filename) {
const a = document.createElement('a')
a.download = filename
a.style.display = 'none'
a.href = href
document.body.appendChild(a)
a.click()
a.remove()
window.URL.revokeObjectURL(href)
}
// 下载excel
downloadExcel({
url: '/default/excel/export',
responseType: 'arraybuffer'
})
responseType设置为arraybuffer
responseTyp设置成blob
不设置responseType,出现乱码
若引入mockjs,其拦截响应,重置了responseType,会出现乱码
总结
此下载excel只适用于post请求,get请求交给浏览器自行解析处理
responseType必须设置成arraybuffer或blob
下载excel时务必关闭mockjs之类的拦截响应的服务
希望本文所述对大家JavaScript程序设计有所帮助。
来源:https://blog.csdn.net/harmsworth2016/article/details/84395241
标签:javascript,Blob对象,下载文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
selenium+python 去除启动的黑色cmd窗口方法
2023-06-07 10:56:27
基于Go语言实现分金币游戏
2023-06-22 06:49:28
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接,错误:“The server selected protocol version TLS10 is not accepted by client
2024-01-14 21:42:28
![](https://img.aspxhome.com/file/2023/2/110942_0s.png)
数据库主键的故事
2008-05-31 07:50:00
Python第三方库qrcode或MyQr生成博客地址二维码
2023-06-25 06:23:08
![](https://img.aspxhome.com/file/2023/9/109429_0s.png)
CSS和HTML与前端技术层图示
2010-04-05 21:54:00
![](https://img.aspxhome.com/file/UploadPic/20104/5/200851918234142177801-87s.gif)
Mysql InnoDB引擎的索引与存储结构详解
2024-01-12 15:22:52
![](https://img.aspxhome.com/file/2023/5/106595_0s.jpg)
Python调用ChatGPT API接口的用法详解
2022-10-11 23:29:25
![](https://img.aspxhome.com/file/2023/3/134443_0s.webp)
Django框架的中的setting.py文件说明详解
2022-11-05 13:26:24
![](https://img.aspxhome.com/file/2023/2/89172_0s.png)
优化MySQL数据库查询的三种方法
2009-03-09 15:19:00
PHP实现的curl批量请求操作示例
2023-11-17 01:51:10
Python利用docx模块实现快速操作word文件
2022-06-19 07:18:45
![](https://img.aspxhome.com/file/2023/8/78538_0s.png)
python与字符编码问题
2022-09-02 01:08:45
![](https://img.aspxhome.com/file/2023/7/105897_0s.jpg)
网站中美好的细节
2011-07-13 18:43:07
python requests.get带header
2022-07-28 20:50:40
![](https://img.aspxhome.com/file/2023/1/97091_0s.jpg)
python虚拟环境模块venv使用及示例
2021-07-21 12:05:48
python tqdm库的使用
2023-10-30 22:32:44
![](https://img.aspxhome.com/file/2023/9/131009_0s.png)
order by newid() 各种数据库随机查询的方法
2024-01-19 16:53:14
如何利用python将Xmind用例转为Excel用例
2022-06-18 19:18:46
![](https://img.aspxhome.com/file/2023/2/133562_0s.png)
DBeaver一款替代Navicat的数据库可视化工具
2024-01-18 07:55:44
![](https://img.aspxhome.com/file/2023/0/132380_0s.jpg)