Browse Source

修改下载txt

unknown 3 years ago
parent
commit
43c551b51e
7 changed files with 260 additions and 31 deletions
  1. 5 4
      admin/algorithm-add.html
  2. 1 0
      admin/algorithm.html
  3. 79 19
      admin/js/algorithm-add.js
  4. 40 6
      admin/js/algorithm.js
  5. 132 0
      admin/js/download2.js
  6. 1 1
      admin/js/flight.js
  7. 2 1
      admin/js/welcome.js

+ 5 - 4
admin/algorithm-add.html

@@ -14,6 +14,7 @@
 		<script src="./lib/layui/layui.js" charset="utf-8"></script>
 		<script src="./js/xadmin.js"></script>
 		<script src="../other/js/functions.js"></script>
+		<script src="./js/download2.js"></script>
 		<script src="./js/algorithm-add.js"></script>
 	</head>
 
@@ -24,10 +25,10 @@
 					<div class="layui-row">
 						<div class="layui-col-sm12">
 							<div class="layui-form-item">
-								<label for="algorithm_type" class="layui-form-label"> 算法类别 </label>
-								<div class="layui-input-inline">
-									<input type="text" id="algorithm_type" name="algorithm_type" required="" lay-verify="required" autocomplete="off"
-									 class="layui-input">
+								<label for="algorithm_typeSelect" class="layui-form-label"> 算法类别 </label>
+								<div class="layui-input-inline  layui-form"  lay-filter="aircraft_typeFilter">
+									 <select name="algorithm_type" id="algorithm_typeSelect"  lay-verify="required">
+									 </select>
 								</div>
 							</div>
 						</div>

+ 1 - 0
admin/algorithm.html

@@ -14,6 +14,7 @@
     <script src="./lib/layui/layui.js" charset="utf-8"></script>
     <script src="./js/xadmin.js"></script>
     <script src="../other/js/functions.js"></script>
+	<script src="./js/download2.js"></script>
     <script src="./js/algorithm.js"></script>
     <style>
         .search {

+ 79 - 19
admin/js/algorithm-add.js

@@ -1,7 +1,34 @@
+let arr = [{
+		id: 0,
+		name: "预处理"
+	},
+	{
+		id: 1,
+		name: "特征提取"
+	},
+	{
+		id: 2,
+		name: "参数优化"
+	},
+	{
+		id: 3,
+		name: "故障诊断"
+	},
+	{
+		id: 4,
+		name: "寿命预测"
+	},
+	{
+		id: 5,
+		name: "其他"
+	},
+]
+var majorId = 0;
 $(function() {
 	var params = window.location.href.split("?");
 	if (params.length > 1) {
 		values = params[1].split("&");
+		
 		if (values.length > 7) {
 			var funcId = values[0].replace("id=", "");
 			var algorithm_type = '';
@@ -13,6 +40,7 @@ $(function() {
 			var remarks = '';
 			if (funcId !== "0") {
 				algorithm_type = decodeURI(values[1].replace("algorithm_type=", ""));
+				// console.log(algorithm_type)
 				algorithm_name = decodeURI(values[2].replace("algorithm_name=", ""));
 				algorithm_format = decodeURI(values[3].replace("algorithm_format=", ""));
 				device_name = decodeURI(values[4].replace("device_name=", ""));
@@ -23,7 +51,7 @@ $(function() {
 		}
 	}
 
-	$("#algorithm_type").val(algorithm_type);
+	// $("#algorithm_typeSelect").find("option:selected").val(algorithm_type);
 	$("#algorithm_name").val(algorithm_name);
 	$("#algorithm_format").val(algorithm_format);
 	$("#device_name").val(device_name);
@@ -35,20 +63,30 @@ $(function() {
 		layui.form.render();
 	});
 
-	layui.use(["form", "layer","upload", "laydate"], function() {
+	layui.use(["form", "layer", "upload", "laydate"], function() {
 		$ = layui.jquery;
 		var form = layui.form,
 			layer = layui.layer;
 		var upload = layui.upload;
 		
-	
+		form.on("select", function(data) {
+		   if (data.elem.id === "algorithm_typeSelect"){
+			     algorithm_type = data.elem[data.elem.selectedIndex].text;
+				 // console.log(algorithm_type)
+		   }
+		    
+		  
+		})
+
 		form.on("submit(save)", function(data) {
 			var func = data.field;
 			func.id = funcId;
+			func.algorithm_type = algorithm_type
 			let url = "";
 			if (func.id === "0") url = "api/algorithm/add";
 			else url = "api/algorithm/edit";
 			fetchJSON(url, func, "post", function(data) {
+				// console.log(data)
 				if (data.status === 0) {
 					var index = parent.layer.getFrameIndex(window.name);
 					showMsg("修改成功", 1, 2000);
@@ -59,24 +97,46 @@ $(function() {
 			});
 			return false;
 		});
-		
+
 		upload.render({
-		    elem: '#file',
-		    url: urlBase + "api/algorithm/importData",
-		    accept: 'file', //普通文件
-		    headers: {
-		        "token": sessionStorage.getItem("token")
-		    },
-		    data: {},
-		    // accept: 'file',
-		    done: function(res) {
-		        if (res.status === 0) {
-		            $("#algorithm_path").val(res.msg);
-		        } else
-		            showMsg(res.msg, 2);
-		    }
+			elem: '#file',
+			url: urlBase + "api/algorithm/importData",
+			accept: 'file', //普通文件
+			headers: {
+				"token": sessionStorage.getItem("token")
+			},
+			data: {},
+			// accept: 'file',
+			done: function(res) {
+				if (res.status === 0) {
+					$("#algorithm_path").val(res.msg);
+				} else
+					showMsg(res.msg, 2);
+			}
 		});
-		
+
+		function getAirType() {
+			// console.log(bet, "121111111")
+			$("#algorithm_typeSelect").empty();
+			$("#algorithm_typeSelect").append($("<option>", {
+				value: -1,
+				text: "请选择"
+			}));
+			for (var i = 0; i < arr.length; i++) {
+				$("#algorithm_typeSelect").append(
+					$("<option>", {
+						value: arr[i].id,
+						text: arr[i].name,
+						// selected: true
+					})
+				);
+			}
+			layui.use("form", function() {
+				layui.form.render('select', 'aircraft_typeFilter');
+			});
+			// form.render('select');
+		}
+		getAirType()
 
 	});
 });

+ 40 - 6
admin/js/algorithm.js

@@ -166,13 +166,47 @@ $(function() {
 			} else
 			if (obj.event === 'download') {
 				var file_path = data.algorithm_path;
+				var newimg = file_path.substr(file_path.indexOf('.'), file_path.length);
 				var newurl = file_path.substr(1, file_path.length);
-				file_path = newurl.substr(newurl.indexOf('\\') + 1, file_path.length);
-				newurl = file_path.substr(0, file_path.indexOf('.'));
-				var newimg = file_path.substr(file_path.indexOf('.'),file_path.length);
-				newurl = urlBase + "sourcekg/" + newurl + newimg;
-				// console.log(newurl)
-				window.open(newurl)
+				var data_file_path = newurl.substr(newurl.indexOf('\\') + 1, file_path.length);
+				var data_newurl = data_file_path.substr(0, data_file_path.indexOf('.'));
+				// console.log(data_newurl)
+				if (newimg ===".txt") {
+					// console.log(newimg)
+					function downloadfile(url, name) {
+						var xmlHttp = null;
+						if (window.ActiveXObject) {
+							// IE6, IE5 浏览器执行代码
+							xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
+						} else if (window.XMLHttpRequest) {
+							// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
+							xmlHttp = new XMLHttpRequest();
+						}
+						//2.如果实例化成功,就调用open()方法:
+						if (xmlHttp != null) {
+							xmlHttp.open("get", url, true);
+							xmlHttp.send();
+							xmlHttp.onreadystatechange = doResult; //设置回调函数
+						}
+						function doResult() {
+							if (xmlHttp.readyState == 4) { //4表示执行完成
+							
+								if (xmlHttp.status == 200) { //200表示执行成功
+									//引用js库:http://danml.com/js/download2.js
+									download(xmlHttp.responseText, name, "text/plain");
+								}
+							}
+						}
+					}
+					downloadfile(urlBase+file_path,data_newurl +'.txt')
+				} else {
+					newurl = urlBase + "sourcekg/" + data_newurl + newimg;
+					// console.log(newurl)
+					// window.open(newurl)
+
+				}
+
+
 
 			}
 		});

+ 132 - 0
admin/js/download2.js

@@ -0,0 +1,132 @@
+//download.js v3.0, by dandavis; 2008-2014. [CCBY2] see http://danml.com/download.html for tests/usage
+// v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime
+// v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs
+// v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support
+
+// data can be a string, Blob, File, or dataURL
+
+		 
+						 
+						 
+function download(data, strFileName, strMimeType) {
+	
+	var self = window, // this script is only for browsers anyway...
+		u = "application/octet-stream", // this default mime also triggers iframe downloads
+		m = strMimeType || u, 
+		x = data,
+		D = document,
+		a = D.createElement("a"),
+		z = function(a){return String(a);},
+		
+		
+		B = self.Blob || self.MozBlob || self.WebKitBlob || z,
+		BB = self.MSBlobBuilder || self.WebKitBlobBuilder || self.BlobBuilder,
+		fn = strFileName || "download",
+		blob, 
+		b,
+		ua,
+		fr;
+
+	//if(typeof B.bind === 'function' ){ B=B.bind(self); }
+	
+	if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback
+		x=[x, m];
+		m=x[0];
+		x=x[1]; 
+	}
+	
+	
+	
+	//go ahead and download dataURLs right away
+	if(String(x).match(/^data\:[\w+\-]+\/[\w+\-]+[,;]/)){
+		return navigator.msSaveBlob ?  // IE10 can't do a[download], only Blobs:
+			navigator.msSaveBlob(d2b(x), fn) : 
+			saver(x) ; // everyone else can save dataURLs un-processed
+	}//end if dataURL passed?
+	
+	try{
+	
+		blob = x instanceof B ? 
+			x : 
+			new B([x], {type: m}) ;
+	}catch(y){
+		if(BB){
+			b = new BB();
+			b.append([x]);
+			blob = b.getBlob(m); // the blob
+		}
+		
+	}
+	
+	
+	
+	function d2b(u) {
+		var p= u.split(/[:;,]/),
+		t= p[1],
+		dec= p[2] == "base64" ? atob : decodeURIComponent,
+		bin= dec(p.pop()),
+		mx= bin.length,
+		i= 0,
+		uia= new Uint8Array(mx);
+
+		for(i;i<mx;++i) uia[i]= bin.charCodeAt(i);
+
+		return new B([uia], {type: t});
+	 }
+	  
+	function saver(url, winMode){
+		
+		
+		if ('download' in a) { //html5 A[download] 			
+			a.href = url;
+			a.setAttribute("download", fn);
+			a.innerHTML = "downloading...";
+			D.body.appendChild(a);
+			setTimeout(function() {
+				a.click();
+				D.body.removeChild(a);
+				if(winMode===true){setTimeout(function(){ self.URL.revokeObjectURL(a.href);}, 250 );}
+			}, 66);
+			return true;
+		}
+		
+		//do iframe dataURL download (old ch+FF):
+		var f = D.createElement("iframe");
+		D.body.appendChild(f);
+		if(!winMode){ // force a mime that will download:
+			url="data:"+url.replace(/^data:([\w\/\-\+]+)/, u);
+		}
+		 
+	
+		f.src = url;
+		setTimeout(function(){ D.body.removeChild(f); }, 333);
+		
+	}//end saver 
+		
+
+	if (navigator.msSaveBlob) { // IE10+ : (has Blob, but not a[download] or URL)
+		return navigator.msSaveBlob(blob, fn);
+	} 	
+	
+	if(self.URL){ // simple fast and modern way using Blob and URL:
+		saver(self.URL.createObjectURL(blob), true);
+	}else{
+		// handle non-Blob()+non-URL browsers:
+		if(typeof blob === "string" || blob.constructor===z ){
+			try{
+				return saver( "data:" +  m   + ";base64,"  +  self.btoa(blob)  ); 
+			}catch(y){
+				return saver( "data:" +  m   + "," + encodeURIComponent(blob)  ); 
+			}
+		}
+		
+		// Blob but not URL:
+		fr=new FileReader();
+		fr.onload=function(e){
+			saver(this.result); 
+		};
+		fr.readAsDataURL(blob);
+	}	
+	return true;
+} /* end download() */
+

+ 1 - 1
admin/js/flight.js

@@ -189,7 +189,7 @@ $(function() {
 				console.log(newimg)
 				newurl = urlBase + "sourcekg/" + newurl + newimg;
 				console.log(newurl)
-				// window.open(newurl)
+				window.open(newurl)
 			
 			}
 		});

+ 2 - 1
admin/js/welcome.js

@@ -210,7 +210,8 @@ $(function() {
 				}
 				fetchJSON('api/experiment/add', data, "post", function(data) {
 					if (data.status === 0) {
-				
+						showMsg("添加成功", 1, 2000);
+						setTimeout('window.location.reload()',3000);
 					} else {
 				
 					}