My Little World

下载文件

下载/导出文件

1.不带参数

window.open(url);

2.带参数

window.open(".../?para1="+para1val+"&para2="+para2val);

前端处理下载数据

H5 chrome支持;IE 11;IE edge 不支持

1
2
3
4
5
6
7
8
9
10
11
12
 download(filename, text) {
var element = document.createElement('a')
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text))
element.setAttribute('download', filename)

element.style.display = 'none'
document.body.appendChild(element)

element.click()

document.body.removeChild(element)
}

使用blob 下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
download(filename, text) {
var aLink = document.createElement('a')
var blob = new Blob([text], {
type: 'text/plain'
})
aLink.download = filename
aLink.href = URL.createObjectURL(blob)
if(!!window.ActiveXObject || 'ActiveXObject' in window){ //兼容IE
window.navigator.msSaveOrOpenBlob(blob, filename)
}else {
aLink.click()
}
URL.revokeObjectURL(blob)
aLink = null
blob = null
},