iPortalUploadData.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <!--********************************************************************
  2. * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
  3. *********************************************************************-->
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8">
  8. <title data-i18n="resources.title_iPortalUploadData"></title>
  9. <style>
  10. #two {
  11. display: none;
  12. }
  13. .component-openfile__span--select {
  14. min-width: 150px!important;
  15. }
  16. .component-openfile__span--select .component-openfile__span {
  17. width: 100px;
  18. overflow: hidden;
  19. text-overflow: ellipsis;
  20. }
  21. </style>
  22. </head>
  23. <body style=" margin: 0;overflow: auto;background: #F2F2F2;width: 100%;height:100%;position: absolute;top: 0;">
  24. <div class="container">
  25. <div class="page-header">
  26. <h4 data-i18n="resources.title_iPortalUploadData"></h4>
  27. </div>
  28. <div class="col-md-10">
  29. <div class="row">
  30. <div class="col-md-12" style="background: #FFF;">
  31. <!--上传数据-->
  32. <div class="row" id="batch-add-resource" style="padding: 10px;">
  33. <div id="one">
  34. <h5 data-i18n="resources.text_data_type"></h5>
  35. <select class='form-control' id='typeSelect-type' onchange="onSelectDataType()">
  36. <option value="WORKSPACE" data-i18n="resources.text_workspace"></option>
  37. <option value="UDB" data-i18n="resources.text_UDB"></option>
  38. <option value="SHP" data-i18n="resources.text_shapefile"></option>
  39. <option value="GEOJSON" data-i18n="resources.text_geojson"></option>
  40. <option value="SMTILES" data-i18n="resources.text_smtiles"></option>
  41. <option value="SVTILES" data-i18n="resources.text_svtiles"></option>
  42. <option value="MBTILES" data-i18n="resources.text_mbtiles"></option>
  43. <option value="TPK" data-i18n="resources.text_tpk"></option>
  44. <option value="UGCV5" data-i18n="resources.text_ugcv5"></option>
  45. <option value="UGCV5_MVT" data-i18n="resources.text_ugcv5_mvt"></option>
  46. <option value="EXCEL" data-i18n="resources.text_excel"></option>
  47. <option value="CSV" data-i18n="resources.text_csv"></option>
  48. <option value="JSON" data-i18n="resources.text_json"></option>
  49. </select>
  50. <h5 data-i18n="resources.text_data_file"></h5>
  51. <div class='file-select'>
  52. <label class='component-openfile__span--select' for='input_file'>
  53. <div class='supermapol-icons-upload'></div>
  54. <span class='component-openfile__span' data-i18n="resources.text_openFile"></span>
  55. </label>
  56. <input class='component-openfile__input' id='input_file' type='file'>
  57. </div>
  58. <h5 data-i18n="resources.text_data_name"></h5>
  59. <input id="data-name" class='form-control' type="text" style="width: 100%;">
  60. <h5 data-i18n="resources.detail_iPortal_tags"></h5>
  61. <input id="data-tags" class='form-control' type="text" style="width: 100%;" data-i18n="[placeholder]resources.text_user_data">
  62. <div><button class="btn btn-primary col-md-offset-6" style="margin-top: 20px;" onclick="inNext()" data-i18n="resources.btn_next" id="upload-or-next"></button></div>
  63. </div>
  64. <div id="two">
  65. <div>
  66. <h5 data-i18n="resources.text_x_field"></h5>
  67. <select id="xFleid" class='form-control' >
  68. </select>
  69. </div>
  70. <div>
  71. <h5 data-i18n="resources.text_y_field"></h5>
  72. <select id="yFleid" class='form-control' >
  73. </select>
  74. </div>
  75. <div id="file-encoding-container">
  76. <h5 data-i18n="resources.text_file_encoding"></h5>
  77. <select class='form-control' id='file-encoding'>
  78. <option value="UTF-8">UTF-8</option>
  79. <option value="GBK">GBK</option>
  80. </select>
  81. </div>
  82. <div>
  83. <button class="btn btn-primary col-md-offset-6" style="margin-top: 20px;" onclick="returnOnePage()" data-i18n="resources.btn_previous"></button>
  84. <button class="btn btn-primary" style="margin-top: 20px;" onclick="buttonSubmit()" data-i18n="resources.btn_upload"></button>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. <script type="text/javascript" include="jquery,bootstrap,widgets" src="../js/include-web.js"></script>
  93. <script type="text/javascript" include="iclient-leaflet-css,xlsx" src="../../dist/leaflet/include-leaflet.js"></script>
  94. <script>
  95. //iportal的URL地址 或根据情况添加自己的本地iportal地址:http://localhost:8190/iportal
  96. var iPortalUrl = "https://iportal.supermap.io/iportal";
  97. var token = "MFEKl8kVmkvEDrDFcgnjodlPCudIO83mBSwvdjiUlWNnySGD-FCEyYEk4CsY2WnSCujfAs8HI0ZkSL6Vxa-njg..";
  98. //上传文件的一些变量(文件对象 文件格式 数据类型 上传的文件名称)
  99. var file,fileFormats,dataType,uploadDataFileName;
  100. //excel csv解析(回调重定向对象 x坐标区域 y坐标区域)
  101. var context = {};
  102. var Xfields = [];
  103. var Yfields = [];
  104. //数据名称
  105. var dataName;
  106. $(document).ready(function () {
  107. generatePortalToken();
  108. //初始化数据类型
  109. onSelectDataType();
  110. //初始化上传的文件名称(选择文件)
  111. uploadDataFileName = resources.text_openFile;
  112. });
  113. //获取token
  114. function generatePortalToken() {
  115. var serverInfo = new SuperMap.ServerInfo(SuperMap.ServerType.IPORTAL, {
  116. server: iPortalUrl
  117. });
  118. SuperMap.SecurityManager.registerServers([serverInfo]);
  119. SuperMap.SecurityManager.destroyToken(iPortalUrl);
  120. SuperMap.SecurityManager.registerToken(iPortalUrl, token);
  121. }
  122. //选中数据类型函数
  123. function onSelectDataType(){
  124. $("#data-tags").val("");
  125. $(".component-openfile__span").text(resources.text_openFile)
  126. $("#data-name").val("")
  127. //获取选中的值
  128. dataType = $("#typeSelect-type").val();
  129. if(dataType === 'EXCEL' || dataType === 'CSV') {
  130. $("#upload-or-next").text(resources.btn_next)
  131. }else {
  132. $("#upload-or-next").text(resources.btn_upload)
  133. }
  134. switch (dataType) {
  135. case 'WORKSPACE':
  136. case 'UDB':
  137. case 'SHP':
  138. case 'UGCV5':
  139. case 'UGCV5_MVT':
  140. fileFormats = "*.zip";
  141. break;
  142. case 'SMTILES':
  143. fileFormats = "*.smtiles|*.zip";
  144. break;
  145. case 'SVTILES':
  146. fileFormats = "*.svtiles|*.zip";
  147. break;
  148. case 'MBTILES':
  149. fileFormats = "*.mbtiles|*.zip";
  150. break;
  151. case 'TPK':
  152. fileFormats = "*.tpk|*.zip";
  153. break;
  154. case 'GEOJSON':
  155. fileFormats = "*.geojson";
  156. break;
  157. case 'EXCEL':
  158. fileFormats = "*.xls|*.xlsx";
  159. break;
  160. case 'CSV':
  161. fileFormats = "*.csv";
  162. break;
  163. case 'JSON':
  164. fileFormats = "*.json";
  165. break;
  166. }
  167. }
  168. //上传文件函数
  169. $("#input_file").change(function(e){
  170. file = e.currentTarget.files[0];
  171. if(file) {
  172. var fileNameLength = file.name.split(".").length;
  173. var result = fileFormats.split("|").filter((item) => {
  174. return item === "*." +file.name.split(".")[fileNameLength-1];
  175. });
  176. if(result.length) {
  177. uploadDataFileName = file.name;
  178. dataName = file.name.substring(0,file.name.lastIndexOf('.'));
  179. }else {
  180. uploadDataFileName = resources.text_openFile;
  181. dataName = '';
  182. widgets.alert.showAlert(resources.msg_file_type, false);
  183. }
  184. }else {
  185. dataName = '';
  186. uploadDataFileName = resources.text_openFile;
  187. }
  188. $("#data-name").val(dataName)
  189. $(".component-openfile__span").text(uploadDataFileName)
  190. })
  191. //获取文件名称的后缀名
  192. function getExtension(fileName) {
  193. if (!fileName) {
  194. return "";
  195. }
  196. if (fileName.indexOf('.') === -1) {
  197. return "";
  198. }
  199. return fileName.substring(fileName.lastIndexOf('.'));
  200. }
  201. //下一步
  202. function inNext(){
  203. if(!file) {
  204. widgets.alert.showAlert(resources.msg_upload_file, false);
  205. return;
  206. }
  207. if($("#data-name").val() === '') {
  208. return;
  209. }
  210. if($("#upload-or-next").text() === resources.btn_upload) {
  211. buttonSubmit();
  212. return;
  213. }
  214. if(uploadDataFileName === resources.text_openFile){
  215. widgets.alert.showAlert(resources.msg_upload_file, false);
  216. return;
  217. }
  218. $("#one").css("display","none");
  219. $("#two").css("display","block");
  220. var isShow;
  221. if(dataType === 'EXCEL' || dataType === 'CSV') {
  222. dataType === 'CSV'?isShow = "block":isShow = "none";
  223. $("#file-encoding-container").css("display",isShow)
  224. visualForm();
  225. }
  226. }
  227. //返回上一步
  228. function returnOnePage(){
  229. $("#one").css("display","block");
  230. $("#two").css("display","none");
  231. }
  232. //上传
  233. function buttonSubmit(){
  234. var params;
  235. var fileNameParam = dataName ? dataName.replace(/(^\s*)|(\s*$)/g, "") : "";
  236. if(!fileNameParam){
  237. fileNameParam = uploadDataFileName;
  238. } else {
  239. //如果文件名后缀不相同则重新赋值
  240. var realFileNameExt = getExtension(uploadDataFileName);
  241. if (realFileNameExt !== getExtension(fileNameParam)) {
  242. fileNameParam = fileNameParam + realFileNameExt;
  243. }
  244. }
  245. var tags = $("#data-tags").val();
  246. params = {
  247. "fileName": fileNameParam,
  248. "tags": tags ? tags.replace(',',',').split(',') : [], // 将中文逗号替换为英文逗号并去重
  249. "type": dataType
  250. }
  251. var queryParams = new SuperMap.iPortalAddDataParam({
  252. fileName: params.fileName,
  253. type: params.type,
  254. tags: params.tags
  255. });
  256. var newDataMetaParams;
  257. if(dataType === 'EXCEL' || dataType === 'CSV') {
  258. newDataMetaParams = {
  259. xField:$("#xFleid").val(),
  260. yField:$("#yFleid").val()
  261. }
  262. if(dataType === 'CSV') {
  263. newDataMetaParams.fileEncoding = $("#file-encoding").val();
  264. }
  265. var dataMetaParams = new SuperMap.iPortalDataMetaInfoParam(newDataMetaParams)
  266. queryParams = new SuperMap.iPortalAddDataParam({
  267. fileName: params.fileName,
  268. type: params.type,
  269. tags: params.tags,
  270. dataMetaInfo:dataMetaParams
  271. });
  272. }
  273. const formData = new FormData();
  274. formData.append('newfile', file);
  275. new SuperMap.iPortalUser(iPortalUrl).addData(queryParams,formData).then(res=>{
  276. if(res.childID) {
  277. widgets.alert.showAlert(resources.msg_upload_success, true);
  278. }else {
  279. widgets.alert.showAlert(resources.msg_upload_failed, false);
  280. }
  281. })
  282. }
  283. //解析excel csv表格数据
  284. function visualForm(){
  285. var files = {
  286. file:file
  287. }
  288. //解析excel csv
  289. SuperMap.Components.FileReaderUtil.readFile(dataType,files,success,failed,context)
  290. }
  291. //Excel CSV数据处理成功的回调
  292. function success(res){
  293. Xfields = [];
  294. Yfields = [];
  295. $("#xFleid").empty();
  296. $("#yFleid").empty();
  297. //把excel csv数据解析为geojson的数据
  298. var result = SuperMap.Components.FileReaderUtil.processExcelDataToGeoJson(res);
  299. //excel csv表头数据
  300. var title = Object.keys(result[0].properties);
  301. //把表格数据为数字的项 做为xField yField的值
  302. title.forEach((item,index)=>{
  303. if(item !== '') {
  304. //使用正则表达式 判断excel表格中某一列是否是数字
  305. if(/^\d+(\.\d+)?$/.test(result[0].properties[item])){
  306. Xfields.push(item);
  307. Yfields.push(item);
  308. }
  309. }
  310. })
  311. //渲染Xfields
  312. Xfields.forEach(item=>{
  313. $("#xFleid").append("<option value='"+item+"'>"+item+"</option>");//新增
  314. })
  315. //渲染Yfields
  316. Yfields.forEach(item=>{
  317. $("#yFleid").append("<option value='"+item+"'>"+item+"</option>");//新增
  318. })
  319. }
  320. //Excel CSV数据处理失败的回调
  321. function failed(res){
  322. console.log(res)
  323. }
  324. </script>
  325. </body>
  326. </html>