upload.js 5.3 KB


var currentPath;
function createHtml(obj) {
	currentPath=$("#currentparam").val();//获取隐藏域中参数
    var htmstr = [];
//    htmstr.push(  "<form id='_fileForm' enctype='multipart/form-data'>");
//    htmstr.push(  "<table cellspacing=\"0\" cellpadding=\"3\" style=\"margin:0 auto; margin-top:20px;\">");
//    htmstr.push(  "<tr>");
//    htmstr.push(  "<td class=\"tdt tdl\">请选择文件:</td>");
//    htmstr.push(  "<td class=\"tdt tdl\"><input id=\"file\" type=\"file\" name=\"file\"/></td>");
//    htmstr.push(  "</tr>");
//    htmstr.push(  "<tr>");
//    htmstr.push(  "<td class=\"tdt tdl tdr\" colspan='2'   style='text-align:right;' ><input type=\"button\" onclick=\"fileloadon()\" value=\"上传\"/></td>");
//    htmstr.push(  "</tr>");
////    htmstr.push(  "<tr> <td class=\"tdt tdl tdr\" colspan='3'style='text-align:center;'><div id=\"msg\">&nbsp;</div></td> </tr>");
////    htmstr.push(  "<tr> <td class=\"tdt tdl tdr\" colspan='3' style=\" vertical-align:middle;\"><div id=\"proce\"></div></td></tr>");
//    htmstr.push(  "</table>")
//    htmstr.push(  "</form>");    
	  htmstr.push('<form id="_fileForm" enctype="multipart/form-data" style="margin-left:16px;margin-top:16px;">');
	  htmstr.push('<input class="text-border" style="width:260px;margin-right:4px;" id="txtFileName" type="text"/>');
	  htmstr.push('<a href="javascript:;" class="a-upload btn btn-default"><input id="file" onchange="onchangeFileName(this)" type="file" name="file"/><span class="glyphicon glyphicon-file"></span>浏览...</a>');
	  htmstr.push('<div id="msg">');
	  htmstr.push('<div id="proce" style="height:10px;"></div>');
	  htmstr.push('<div style="margin:20px;text-align: right;">');
	  htmstr.push(  "<div id=\"progress\"><div id=\"bar\"></div><div id=\"percent\">0%</div > </div> <div id=\"message\"></div> ");
	  htmstr.push('<button class="btn btn-default" style="margin-right:10px;" onclick="closewindow()">取消</button>');
	  htmstr.push('<input type="button" class="btn btn-info" onclick="fileloadon()" value="上传"/>');
	  htmstr.push('</div>');
	  htmstr.push('</form>')
	  obj.html(htmstr.join(""));
}
//触发浏览文件
function triggerBrowerFile(){
	$("#file").trigger("click");
}
//文件选中后修改文件
function onchangeFileName(obj){
	$("#txtFileName").val($("#file").val());
}
var iphost="http://116.10.196.223:8080/doc-service/";
var oTimer = null;
var md5str = "";
var filename="";
function fileloadon() {
//    currentPath =getUrlParam('currentPath');
	filename = document.getElementById("file").value;
	md5str = hex_md5(filename+new Date().getTime());
    url=iphost+"ftpfile/uploadfile.do?md5str="+md5str;
    $("#_fileForm").ajaxSubmit({
        type: "post",
        data: {
            'dirPath': currentPath
        },
        url:url ,
        beforeSend: function() 
		{ 
			$("#progress").show(); 
			//clear everything 
			$("#bar").width('0%'); 
			$("#message").html(""); 
			$("#percent").html("0%"); 

			oTimer = setInterval("getProgress()", 100);
		}, 
//        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        success: function (data) {
        	window.parent.layer.msg("上传成功!",{icon:1});
        	window.parent.listFiles(currentPath);
        	$('#_fileForm').clearForm();
        	window.parent.layer.close(window.parent.index_attach);
      $('#proce').progressbar('setValue', 100);
      $('#proce').hidden();
        },
        error: function (msg) {
        	window.parent.layer.msg("文件上传失败!",{icon:0});  
            window.parent.layer.close(window.parent.index_attach);
        }
        /**,
        beforeSend: function() { //开始上传 
        	uploadprogress();
        }**/
    });
	
//    uploadprogress();
    return false;
}

function getProgress() {
	var now = new Date();
    $.ajax({
        type: "post",
        dataType: "json",
        url: iphost+"ftpfile/getuploadprocess.do?md5str="+md5str,
        data: now.getTime(),
        success: function(data) {
			console.log("get progress data is:"+data.message);
        	$("#bar").width(data.message); 
			$("#percent").html(data.message);
        },
        error: function(err) {
        	console.log("get progress data fail:"+err); 
        }
    });
}

//function uploadprogress()
//{
//	var flag=0;
//	$('#proce').progressbar({
//		width : 200,
//		height : 10,
//		value : 0,
//		text : '{value}%',
//		onChange : function (newValue, oldValue) {
//			console.log('新:' + newValue + ',旧:' + oldValue);
// 			if(newValue==100)
//				{
// 				 clearInterval(timer);
//				$('#proce').hide();
//				$('#proce').progressbar('setValue',0);
//				}
//		},
//	});
//	$('#proce').show();
//	var timer = setInterval(function () {
//		if($('#proce').progressbar('getValue')<100){	
//		var url=iphost+'ftpfile/getuploadprocess.do';
//		var filename=currentPath;
//		if(flag<100){
//		$.post(url,{filename:filename}, function (result) {
//			  if(result)
//			  {			 
//				  $('#proce').progressbar('setValue', result);
//				  flag=result;					 
//				  return;
//			  }else{			  
//			  }
//		});
//		
//		}
//		}
//	}, 200);
//}


function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}