1. 程式人生 > >FileUpload 上傳下載檔案相容IE8

FileUpload 上傳下載檔案相容IE8

專案上使用FileUpload 上傳附件,樣式比較簡單,功能也比較簡單。簡單的封裝了一下

function AttachComponent(field, instanceId, value) {
	this.field = field;
	this.fieldName = this.field.name;
	this.fieldId = "id-" + Math.random() * 10e18 + "-" + this.fieldName;
	this.showName = this.field.showName;
	this.instanceId = instanceId;
	this.value = value;
	this.initializeDom();
}

AttachComponent.prototype.initializeDom = function() {
	var renderType = getFormInstanceRenderType();
	if (renderType == 'CREATE') {
		this.createDom();
	} else if (renderType == 'EDIT') {
		this.editDom();
	} else {
		this.approveDom();
	}
	// this.brush();
};

AttachComponent.prototype.getDom = function() {
	return this.dom;
};

AttachComponent.prototype.createDom = function() {
	if (this.value == null || this.value == undefined || this.value == "") {
		this.value = "";
	}
	var domHtml = new com.isprint.utils.StringBuilder();
	domHtml.append("<div style='clear:both;padding-top:5px;'>");
	domHtml.append("<div class='easyui-panel' title='附件上傳' > ");
	domHtml
			.append("<table cellspacing='0' cellpadding='0' style='border: 0px;padding:5px'>");
	domHtml.append("<tr style='height:25px'>");
	domHtml.append("<th><label for='Attachment_GUID'>附件上傳:</label></th><td>");
	domHtml.append("<div>");
	domHtml
			.append("<input class='easyui-validatebox' type='hidden' id='Attachment_GUID' name='Attachment_GUID' />");
	domHtml.append("<input id='" + this.fieldId
			+ "' name='file' type='file' multiple='multiple'>");
	domHtml
			.append("<a href='javascript:void(0)' class='easyui-linkbutton' id='btnUpload' data-options='plain:true,iconCls:\"icon-save\"' onclick='AttachComponent.prototype.ajaxFileUpload(\""
					+ this.fieldId + "\")'>上傳</a>");
	domHtml.append("<div id='fileQueue' class='fileQueue'></div>");
	domHtml.append("<div id='div_files'></div>");
	domHtml.append("<br />");
	domHtml.append("</div>");
	domHtml.append("</td>");
	domHtml.append("</tr></table>");
	domHtml.append("<table id='uploadFileList'></table>");
	domHtml.append("</div>");
	domHtml.append("</div>");
	this.dom = $(domHtml.toString());

	var dom = this.dom;
	dom
			.find('#uploadFileList')
			.datagrid(
					{
						title : '附件上傳列表資訊',
						nowrap : false,
						striped : true,
						collapsible : false,
						remoteSort : true,
						idField : 'id',
						singleSelect : true,
						rownumbers : true,
						columns : [ [
								{
									field : 'id',
									title : 'ID',
									width : 120,
									hidden:true
//									formatter : function(value, row, index) {
//										return "<a href='#' class='easyui-linkbutton' onclick='AttachComponent.download(\""
//												+ value
//												+ "\")'>"
//												+ value
//												+ "</a>";
//									}
								}, {
									field : 'fileName',
									title : '檔名稱',
									width : 120,
									height : 'auto',
									formatter : function(value, row, index) {
										return "<a href='#' class='easyui-linkbutton' onclick='AttachComponent.download(\""
												+ row.id
												+ "\")'>"
												+ value
												+ "</a>";
									}
								}, {
									field : 'filePath',
									title : '上傳路徑',
									width : 200,
									height : 'auto'
								}, {
									field : 'createBy',
									title : '上傳人',
									width : 120,
									height : 'auto'
								}, {
									field : 'creatTime',
									title : '上傳時間',
									width : 240,
									height : 'auto',
									formatter : function(value, row, index) {
										return timeLongToString(value);
									}
								} ] ]
					});

};
AttachComponent.prototype.editDom = function() {
	if (this.value == null || this.value == undefined || this.value == "") {
		this.value = "";
	}
	var domHtml = new com.isprint.utils.StringBuilder();
	domHtml.append("<div style='clear:both;padding-top:5px;'>");
	domHtml.append("<div class='easyui-panel' title='附件上傳' > ");
	domHtml
			.append("<table cellspacing='0' cellpadding='0' style='border: 0px;padding:5px'>");
	domHtml.append("<tr style='height:25px'>");
	domHtml.append("<th><label for='Attachment_GUID'>附件上傳:</label></th><td>");
	domHtml.append("<div>");
	domHtml
			.append("<input class='easyui-validatebox' type='hidden' id='Attachment_GUID' name='Attachment_GUID' />");
	domHtml.append("<input id='" + this.fieldId
			+ "' name='file' type='file' multiple='multiple'>");
	domHtml
			.append("<a href='javascript:void(0)' class='easyui-linkbutton' id='btnUpload' data-options='plain:true,iconCls:\"icon-save\"' onclick='AttachComponent.prototype.ajaxFileUpload(\""
					+ this.fieldId + "\")'>上傳</a>");
	domHtml.append("<div id='fileQueue' class='fileQueue'></div>");
	domHtml.append("<div id='div_files'></div>");
	domHtml.append("<br />");
	domHtml.append("</div>");
	domHtml.append("</td>");
	domHtml.append("</tr></table>");
	domHtml.append("<table id='uploadFileList'></table>");
	domHtml.append("</div>");
	domHtml.append("</div>");
	this.dom = $(domHtml.toString());

	var dom = this.dom;
	dom
			.find('#uploadFileList')
			.datagrid(
					{
						title : '附件上傳列表資訊',
						nowrap : false,
						striped : true,
						collapsible : false,
						remoteSort : true,
						idField : 'id',
						singleSelect : true,
						rownumbers : true,
						columns : [ [
								{
									field : 'id',
									title : 'ID',
									width : 120,
									hidden:true,
//									formatter : function(value, row, index) {
//										return "<a href='#' class='easyui-linkbutton' onclick='AttachComponent.download(\""
//												+ value
//												+ "\")'>"
//												+ value
//												+ "</a>";
//									}
								}, {
									field : 'fileName',
									title : '檔名稱',
									width : 120,
									height : 'auto',
									formatter : function(value, row, index) {
										return "<a href='#' class='easyui-linkbutton' onclick='AttachComponent.download(\""
												+ row.id
												+ "\")'>"
												+ value
												+ "</a>";
									}
								}, {
									field : 'filePath',
									title : '上傳路徑',
									width : 200,
									height : 'auto'
								}, {
									field : 'createBy',
									title : '上傳人',
									width : 120,
									height : 'auto'
								}, {
									field : 'creatTime',
									title : '上傳時間',
									width : 240,
									height : 'auto',
									formatter : function(value, row, index) {
										return timeLongToString(value);
									}
								} ] ]
					});
	this.uploadList = {};
	this.uploadList.Dom = dom.find('#uploadFileList');
	var uploadListData = this.value;
	this.uploadList.Dom.datagrid('loadData', []);
	this.uploadList.Dom.datagrid('loadData', uploadListData);
};
AttachComponent.download = function(id) {
	window.location.href = getWebContext() + "/api/file/download?fileId=" + id;
};

AttachComponent.prototype.approveDom = function() {
	if (this.value == null || this.value == undefined || this.value == "") {
		this.value = "";
	}
	if (this.value == null || this.value == undefined || this.value == "") {
		this.value = "";
	}
	var domHtml = new com.isprint.utils.StringBuilder();
	domHtml
			.append("<div style='clear:both;padding:5px 3px 5px 0px;width:99%;clear:both;height:auto;'>");
	domHtml.append("<table id='uploadFileList'></table>");
	domHtml.append("</div>");
	this.dom = $(domHtml.toString());
	var dom = this.dom;
	dom
			.find('#uploadFileList')
			.datagrid(
					{
						title : '附件上傳列表資訊',
						nowrap : false,
						striped : true,
						collapsible : false,
						remoteSort : true,
						idField : 'id',
						singleSelect : true,
						rownumbers : true,
						columns : [ [
								{
									field : 'id',
									title : 'ID',
									width : 120,
									hidden:true
//									formatter : function(value, row, index) {
//										return "<a href='#' class='' onclick='AttachComponent.download(\""
//												+ value
//												+ "\")'>"
//												+ value
//												+ "</a>";
//									}
								}, {
									field : 'fileName',
									title : '檔名稱',
									width : 120,
									height : 'auto',
									formatter : function(value, row, index) {
										return "<a href='#' class='' onclick='AttachComponent.download(\""
												+ row.id
												+ "\")'>"
												+ value
												+ "</a>";
									}
								}, {
									field : 'filePath',
									title : '上傳路徑',
									width : 200,
									height : 'auto'
								}, {
									field : 'createBy',
									title : '上傳人',
									width : 120,
									height : 'auto'
								}, {
									field : 'creatTime',
									title : '上傳時間',
									width : 240,
									height : 'auto',
									formatter : function(value, row, index) {
										return timeLongToString(value);
									}
								} ] ]

					});

	this.uploadList = {};
	this.uploadList.Dom = dom.find('#uploadFileList');
	var uploadListData = this.value;
	this.uploadList.Dom.datagrid('loadData', []);
	if (uploadListData && uploadListData.length > 0) {
		this.uploadList.Dom.datagrid('loadData', uploadListData);
	}
};


AttachComponent.prototype.ajaxFileUpload = function(fieldId) {
	var uploadUrl = getWebContext() + '/api/file/upload';

	$.ajaxFileUpload({
		url : uploadUrl,// 用於檔案上傳的伺服器端請求地址
		secureuri : false,// 一般設定為false
		type : 'post',
		fileElementId : fieldId,// 檔案上傳空間的id屬性 <input type="file" id="file"
								// name="file" />
		dataType : 'json', // 返回值型別 一般設定為json
		success : function(data, status) { // 伺服器成功響應處理函式
			if (data) {
				var rows = $("#uploadFileList").datagrid("getRows");
				var index = rows.length;
				var obj = data.file;
				$('#uploadFileList').datagrid('insertRow', {
					index : (index + 1),
					row : {
						id : obj.id,
						fileName : obj.fileName,
						filePath : obj.filePath,
						createBy : obj.createBy,
						creatTime : obj.creatTime
					}
				});
			} else {
				alert("新增失敗!");
			}
		},
		error : function(data, status, e) {
			/** 伺服器響應失敗處理函式* */
			alert(e);
		}
	});
};
AttachComponent.prototype.processData = function() {
	var self = this;
	var dom = this.getDom();
	var uploadList = [];
	var dataList = dom.find("#uploadFileList").datagrid('getData').rows;
	if (dataList != undefined && dataList != null) {
		for ( var i = 0; i < dataList.length; ++i) {
			if (dataList != null)
				uploadList.push(dataList[i]);
		}
		return JSON.stringify(uploadList);
	} else {
		return "[]";
	}

};

附件上傳功能還可以,但在IE和谷歌還會有一些問題。