Blame view

src/main/webapp/js/home-content/upload.js 5.3 KB
caiyongsong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151


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; //返回参数值
}