티스토리 뷰

Skill/spring

[spring] 파일업로드 fileupload

진열사랑 2021. 3. 6. 10:32

[front] 

 

** jsp

  : name="files[]" 이름 고정으로 생각

  : 여러 파일 한번에 올리려면 multiple="multiple"

<li><label for="uploadFile">첨부파일</label></li>
<li><input type="file" id="uploadFile" name="files[]" multiple="multiple"  style="margin-top:6px;">
	<ul id="filelist" style="margin-top:10px;"></ul></li>

** js

include : jquery.form-3.51.0.js

var cmFile = {
	list : function(_tblNm,_tblSn){ 
		var dfd = $.Deferred();
		var param = {tblNm : _tblNm, tblSn:_tblSn};
		$.ajax({
	    	url: "/cmfile/list", 
	    	type : "POST",
	    	data: JSON.stringify(param),
	    	dataType : "json",
	    	contentType : "application/json;charset=UTF-8",
	    	success: function (result) {
	    		//console.log(result);
	    		if(!result.list) { 
	    			return dfd.reject();
	    		} else {
	    			return dfd.resolve(result.list);
	    		}
	        }
			,error : function(request, error) {
				return dfd.reject(new Error("message: " + request.responseText + ", error:" + error));
			}
		});
		return dfd.promise();
	},
	getCmFile : function(_tblNm,_tblSn){ 
		var dfd = $.Deferred();
		var param = {tblNm : _tblNm, tblSn:_tblSn};
		$.ajax({
	    	url: "/cmfile/getCmFile", 
	    	type : "POST",
	    	data: JSON.stringify(param),
	    	dataType : "json",
	    	contentType : "application/json;charset=UTF-8",
	    	success: function (result) {
	    		//console.log(result);
	    		if(!result.data) { 
	    			return dfd.reject();
	    		} else {
	    			return dfd.resolve(result.data);
	    		}
	        }
			,error : function(request, error) {
				return dfd.reject(new Error("message: " + request.responseText + ", error:" + error));
			}
		});
		return dfd.promise();
	},
	fileDownload : function (savePath,saveFileNm,realFileNm) {
		if(savePath!=null&&saveFileNm!=null&&realFileNm!=null){
			location.href = "/cmfile/fileDownload?savePath="+savePath+"&saveFileNm="+saveFileNm+"&realFileNm="+realFileNm;
		}
	},
	//개별 다운로드 
	fn_fileDownload : function(_url) {
		$.fileDownload( _url,{
			httpMethod: "GET",
			successCallback: function (url) {
			},
			failCallback: function(responseHtml, url) {
				alert('파일 다운로드 실패!\n관리자에게 문의 주세요.');
			}
		});
	},
	delete : function(_tblNm,_tblSn){ 
		var dfd = $.Deferred();
		var param = {tblNm : _tblNm, tblSn:_tblSn};
		$.ajax({
	    	url: "/cmfile/delete", 
	    	type : "DELETE",
	    	data: JSON.stringify(param),
	    	dataType : "json",
	    	contentType : "application/json;charset=UTF-8",
	    	success: function (result) {
	    		//console.log(result);
	    		if(result.msg=='실패하였습니다.') { 
	    			return dfd.reject();
	    		} else {
	    			return dfd.resolve(result.msg);
	    		}
	        }
			,error : function(request, error) {
				return dfd.reject(new Error("message: " + request.responseText + ", error:" + error));
			}
		});
		return dfd.promise();
	},
	deleteOne : function(_fileSn){ 
		var dfd = $.Deferred();
		var param = {fileSn : _fileSn};
		$.ajax({
	    	url: "/cmfile/deleteOne", 
	    	type : "DELETE",
	    	data: JSON.stringify(param),
	    	dataType : "json",
	    	contentType : "application/json;charset=UTF-8",
	    	success: function (result) {
	    		//console.log(result);
	    		if(result.msg=='실패하였습니다.') { 
	    			return dfd.reject();
	    		} else {
	    			return dfd.resolve(result.msg);
	    		}
	        }
			,error : function(request, error) {
				return dfd.reject(new Error("message: " + request.responseText + ", error:" + error));
			}
		});
		return dfd.promise();
	}
	
}



function fileUploadForm(){
	$("#fileDiv").empty();
	$("#fileDiv").append('<input type="file" name="files[]" id="file_input" multiple="multiple" data-jfiler-limit="3"/>' );
	$('#file_input').filer({
		showThumbs: true,
		addMore: false,
		allowDuplicates: false,
		captions: {
            button: "파일 선택",
            feedback: "",
            feedback2: "개의 파일이 선택되었습니다.",
            drop: "Drop file here to Upload",
            removeConfirmation: "삭제하시겠습니까?",
            errors: {
                filesLimit: "{{fi-limit}}개 파일까지 업로드 가능합니다.",
                filesType: "Only Images are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-fileMaxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB.",
                folderUpload: "You are not allowed to upload folders."
            }
        }
	});
}

function fileDownloadForm(file) {
	var layout = "";
	layout +='<a class="pop_btn" onclick="fileDownload(\''+file.savePath+'\',\''
		+file.saveFileNm+'\',\''+file.realFileNm+'\')">'+file.realFileNm+'</a>';
	return layout;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함