admin管理员组文章数量:1659044
谷歌内核正常,IE下载无后缀名,打不开
- 本次项目需要下载的文件就两种格式xlsx和zip
- 所以走ie时手动添加后缀名就可以搞定了
export const download = async({ url, params, method = 'post', type = 'xlsx' }) => {
const isget = method.toLowerCase() === 'get';
const obj = isget ? { params } : { data: params };
const baseURL = process.env.VUE_APP_BASE_API + url
try {
const { data } = await axios({ baseURL, method, ...obj })
if (data.code) {
window.alert(data.message || '网络繁忙,请稍后重试');
} else {
axios({ baseURL, method, ...obj, responseType: 'arraybuffer' })
.then(res => {
res._params = params
_download(res, type);
}).catch(err => {
window.alert(err.message || '网络繁忙,请稍后重试');
})
}
} catch (error) {
console.log(error);
}
}
const _download = (res, _type) => {
const type = _type === 'xlsx' ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' : 'application/zip';
const fuffix = _type === 'xlsx' ? '.xlsx' : '.zip'
const content = res.data
const blob = new Blob([content], { type });
const fileName = res._params.fileName || '';
if ('download' in document.createElement('a')) { // 非IE下载
const elink = document.createElement('a')
elink.download = window.decodeURIComponent(fileName);
elink.style.display = 'none'
elink.href = window.URL.createObjectURL(blob)
document.body.appendChild(elink)
elink.click()
window.URL.revokeObjectURL(elink.href) // 释放URL 对象
document.body.removeChild(elink)
} else { // IE10+下载
navigator.msSaveBlob(blob, fileName + fuffix )
}
}
版权声明:本文标题:IE下载文件流,文件后缀名丢失 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1726424885a1070254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论