导出excel
fetch版
exportFn(url, folderName) {
fetch(url, {
method: 'get',
responseType: 'blob'
}).then(res => res.blob())
.then(blob => {
let a = document.createElement('a')
// 获取 blob 本地文件连接 (blob 为纯二进制对象,不能够直接保存到磁盘上)
let url = window.URL.createObjectURL(newBlob([blob], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }));
a.href = url;
//定义导出的文件名
a.download = `${folderName}.xls`;
a.click();
window.URL.revokeObjectURL(url);
})
}
axios版
exportFn(url, folderName) {
axios({
method: 'get',
url: url,
responseType: "blob"
}).then(res => {
if (res.type === 'application/vnd.ms-excel') {
const blob = newBlob([res]);
const a = document.createElement("a");
const href = window.URL.createObjectURL(blob);
a.href = href;
a.download = folderName + '.xlsx';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(href);
}
}).catch(function (error) {
console.log(error);
});
}
图片预览
previewImg(url) {
fetch(url, {
method: 'get',
responseType: 'blob'
}).then(res => res.blob())
.then(blob => {
// 获取 blob 本地文件连接 (blob 为纯二进制对象,不能够直接保存到磁盘上)
let imgUrl = window.URL.createObjectURL(newBlob([blob], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }));
return imgUrl
})
}
kml转geojson
getKml(url) {
fetch(url,{method:'get',responseType: 'blob'}).then(res=>res.blob()).then(blob => {
const reader = newFileReader();
reader.readAsText(blob,'utf-8');
reader.onload = function (e) {
const xml = newDOMParser().parseFromString(e.target.result,'text/xml')
const geojson = togeojson.kml(xml, {
style: true
})
return geojson
}
})
}