Gui*_*tto 2 javascript angularjs leaflet
我正在尝试在我的传单地图上显示来自巴西的形状文件,该形状文件位于 .zip 中,其中包含:.dbf、.prj、.sbn、.sbx、.shp 和 .shx 文件。为此,我正在使用:https : //github.com/calvinmetcalf/leaflet.shapefile
我的本地计算机上有 .zip 文件,所以我只是做了:
HTML
<button ng-click="addShape()"> Brasil Shape File </button>
Run Code Online (Sandbox Code Playgroud)
JS
var mymap = L.map('mapid').setView([-12.85, -50.09], 4);
$scope.addShape = function () {
var shpfile = new L.Shapefile('scripts/ShapeFiles/Brasil.zip');
shpfile.addTo(mymap);
}
Run Code Online (Sandbox Code Playgroud)
现在我想让用户上传 .zip 以将其显示在地图上,就像这里发生的事情一样:http : //leaflet.calvinmetcalf.com/#3/32.69/10.55
但我想不通……我在互联网上找到的只是将 .zip 文件发布到一个 url。我需要在用户将其“上传”到浏览器后立即使用该文件。
在下面的代码中,用户可以上传一些文件并发布它,我尝试在发送之前对包含 .zip 文件的假定对象进行 console.log 记录,但我在对象中找不到它:
HTML
<body ng-controller="FileUploadCtrl">
<div class="row">
<label for="fileToUpload">Select a File to Upload</label><br />
<input type="file" ng-model-instant id="fileToUpload" multiple onchange="angular.element(this).scope().setFiles(this)" />
</div>
<input type="button" ng-click="uploadFile()" value="Upload" />
</body>
Run Code Online (Sandbox Code Playgroud)
JS
scope.setFiles = function(element) {
scope.$apply(function(scope) {
console.log('files:', element.files);
// Turn the FileList object into an Array
scope.files = []
for (var i = 0; i < element.files.length; i++) {
scope.files.push(element.files[i])
}
scope.progressVisible = false
});
};
scope.uploadFile = function() {
var fd = new FormData()
for (var i in scope.files) {
fd.append("uploadedFile", scope.files[i])
}
var xhr = new XMLHttpRequest()
xhr.upload.addEventListener("progress", uploadProgress, false)
xhr.addEventListener("load", uploadComplete, false)
xhr.addEventListener("error", uploadFailed, false)
xhr.addEventListener("abort", uploadCanceled, false)
xhr.open("POST", "/fileupload")
scope.progressVisible = true
xhr.send(fd)
}
Run Code Online (Sandbox Code Playgroud)
源小提琴:danielzen
在这个例子中,在函数 'setFiles' 和 'uploadFile' 中,当我 console.log(fd) 我得到:fd: [object FormData]和 console.log(element.files):
element.files[0] File {name: "Brasil (1).zip", lastModified: 1492436239000, lastModifiedDate: Mon Apr 17 2017 10:37:19 GMT-0300 (BRT), webkitRelativePath: "", size: 5988862…}
Run Code Online (Sandbox Code Playgroud)
但是我找不到上传的原始 .zip 文件,可能是因为这不是正确的方法...如果有人知道获取此 .zip 文件的方法或可以访问此示例源并可以共享和我在一起我会很感激的。
我设法通过为 shapefile 使用 arrayBuffer 来解决这个问题,因为它在此处指定。我想通了阅读这个问题。这是我的代码:
HTML
<div id="mapid" style="width: 800px;float: left;">
</div>
<form action='#' onsubmit="return false;">
<input type='file' id='fileinput'>
<input type='button' id='btnLoad' value='Load' onclick='loadFile();'>
</form>
Run Code Online (Sandbox Code Playgroud)
JS
function loadFile() {
input = document.getElementById('fileinput');
if (!input.files[0]) {
bodyAppend("p", "Please select a file before clicking 'Load'");
}
else {
file = input.files[0];
fr = new FileReader();
fr.onload = receiveBinary;
fr.readAsArrayBuffer(file);
}
function receiveBinary() {
result = fr.result
var shpfile = new L.Shapefile(result);
shpfile.addTo(mymap);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3831 次 |
| 最近记录: |