上传geojson文件解析
2021-02-10 14:18
标签:targe cto catch ges 文件 try 文件解析 ret 检查 上传geojson文件解析 标签:targe cto catch ges 文件 try 文件解析 ret 检查 原文地址:https://www.cnblogs.com/liuhao-web/p/13050537.html parseGeson(context, fileInputId) {
return new Promise(((resolve) => {
var inputFile = document.querySelector(`#${fileInputId}`);
inputFile.addEventListener("change", (event) => {
var uri = event.target.value;
if (!/.geojson/ig.test(uri)) {
context.$message({
type: "warning",
message: "请上传geojson文件"
});
document.querySelector(`#${fileInputId}`).value = "";
return;
}
var reader = new FileReader();
reader.readAsText(inputFile.files[0]);
reader.onload = (evt) => {
document.querySelector(`#${fileInputId}`).value = "";
var fileString = evt.target.result;
var result = {
code: "",
data: ""
};
try {
result.data = JSON.parse(fileString);
result.code = 200;
resolve(result);
context.$message({
type: "success",
message: "上传成功"
});
} catch (error) {
context.$message({
type: "warning",
message: "json解析异常,请检查内容的格式是否正确"
});
result.data = fileString;
result.code = 400;
}
resolve(result);
};
}, false);
}));
}