阅读 13
^ 关注我,带你一起学GIS ^
注:当前使用的是 ol [9.2.4] 版本,天地图使用的
key
请到天地图官网申请,并替换为自己的key
前言
在WebGIS系统开发中,通常需要实现文件上传功能,这只是第一步,在实现文件上传进行分析之后,还需要将分析的结果保存下来,这就需要实现文件下载的功能。
1. 文件下载关键代码
文件下载的关键代码在于创建a元素。因为是临时元素,所以将其style
属性display
设置为”none”,并将其添加到body
中。之后通过fetch
方法读取文件数据,将文件内容转换为blob
类型,最后赋值给a
元素属性,紧接着将a
元素从body
中移除。
// 下载文件方法
const linkEle = document.createElement("a")
linkEle.style.display = "none"
document.body.appendChild(linkEle)
function downloadFile(fullPath, fileName) {
fetch(fullPath)
.then(response => response.blob())
.then(blob => {
linkEle.href = URL.createObjectURL(blob)
linkEle.download = fileName
linkEle.click()
document.body.removeChild(linkEle)
})
}
2. 完整代码
其中libs
文件夹下的包需要更换为自己下载的本地包或者引用在线资源。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OpenLayers 文件下载</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="../../libs/css/ol9.2.4.css">
<script src="../../js/config.js"></script>
<script src="../../libs/js/ol9.2.4.js"></script>
<style>
* {
padding: 0;
margin: 0;
font-size: 14px;
font-family: '微软雅黑';
}
html,
body {
width: 100%;
height: 100%;
}
#map {
position: absolute;
top: 50px;
bottom: 0;
width: 100%;
}
#top-content {
position: absolute;
width: 100%;
height: 50px;
line-height: 50px;
background: linear-gradient(135deg, #ff00cc, #ffcc00, #00ffcc, #ff0066);
color: #fff;
text-align: center;
font-size: 32px;
}
#top-content span {
font-size: 32px;
}
.download-file {
position: absolute;
right: 50px;
top: 80px;
padding: 20px 10px;
color: #fff;
border-radius: 5px;
border: 1px solid #50505040;
background: linear-gradient(135deg, #c850c0, #4158d0);
}
.upload-shp:hover {
cursor: pointer;
filter: brightness(120%);
background: linear-gradient(135deg, #eb3ddf, #0931fb);
}
.download-btn {
border-radius: 5px;
border: 1px solid #50505040;
padding: 5px 20px;
color: #fff;
margin: 0 10px;
background: #4646466e;
transition: background-color 10s ease-in-out 10s;
}
.download-btn:hover {
cursor: pointer;
filter: brightness(120%);
background: linear-gradient(135deg, #c850c0, #4158d0);
}
</style>
</head>
<body>
<div id="top-content">
<span>OpenLayers 文件下载</span>
</div>
<div id="map" title="地图显示"></div>
<div class="download-file">
<span id="download-gpx" class="download-btn">GPX</span>
<span id="download-json" class="download-btn">GeoJSON</span>
<span id="download-kml" class="download-btn">KML</span>
</div>
</body>
</html>
<script>
//地图投影坐标系
const projection = ol.proj.get('EPSG:3857');
//==============================================================================//
//============================天地图服务参数简单介绍==============================//
//================================vec:矢量图层==================================//
//================================img:影像图层==================================//
//================================cva:注记图层==================================//
//======================其中:_c表示经纬度投影,_w表示球面墨卡托投影================//
//==============================================================================//
const TDTImgLayer = new ol.layer.Tile({
title: "天地图影像图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=" + TDTTOKEN,
attibutions: "天地图影像描述",
crossOrigin: "anoymous",
wrapX: false
})
})
const TDTImgCvaLayer = new ol.layer.Tile({
title: "天地图影像注记图层",
source: new ol.source.XYZ({
url: "http://t0.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=" + TDTTOKEN,
attibutions: "天地图注记描述",
crossOrigin: "anoymous",
wrapX: false
})
})
const map = new ol.Map({
target: "map",
loadTilesWhileInteracting: true,
view: new ol.View({
center: [102.845864, 25.421639],
zoom: 6.5,
worldsWrap: false,
minZoom: 1,
maxZoom: 20,
projection: 'EPSG:4326',
}),
layers: [TDTImgLayer],
// 地图默认控件
controls: ol.control.defaults.defaults({
zoom: false,
attribution: false,
rotate: false
})
})
map.on('click', evt => {
console.log("获取地图坐标:", evt.coordinate)
})
// 下载文件方法
const linkEle = document.createElement("a")
linkEle.style.display = "none"
document.body.appendChild(linkEle)
function downloadFile(fullPath, fileName) {
fetch(fullPath)
.then(response => response.blob())
.then(blob => {
linkEle.href = URL.createObjectURL(blob)
linkEle.download = fileName
linkEle.click()
document.body.removeChild(linkEle)
})
}
document.querySelector("#download-gpx").addEventListener('click', (evt) => {
downloadFile("http://localhost/DataSource/location.gpx", 'location.gpx')
})
document.querySelector("#download-json").addEventListener('click', (evt) => {
downloadFile("http://localhost/DataSource/lands.json", 'lands.json')
})
document.querySelector("#download-kml").addEventListener('click', (evt) => {
downloadFile("http://localhost/DataSource/subway.kml", 'subway.kml')
})
</script>
❝
OpenLayers示例数据下载,请在公众号后台回复:ol数据
全国信息化工程师-GIS 应用水平考试资料,请在公众号后台回复:GIS考试
❝
GIS之路公众号已经接入了智能助手,欢迎大家前来提问。
欢迎访问我的博客网站-长谈GIS:
http://shanhaitalk.com
都看到这了,不要忘记点赞、收藏+关注 哦!
本号不定时更新有关 GIS开发 相关内容,欢迎关注