메뉴 건너뛰기

조회 수 599 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

hr(XMLHttpRequest) 객체는 서버와 데이터를 확인하기 위해 사용됩니다.

 

ajax또한 xhr 규격을 사용하여 동작하고 있고, 다양한 메소드를 통해 요청의 상태값이나 시간, 결과, 진행상태 등을 확인 할 수 있습니다.

 

여기서 xhr의 upload.onprogress 메소드를 사용하여 파일의 업로드 진행상황을 확인하고 UI적으로 페이지 진행상태를 표현 할 수 있습니다.

 

대량의 파일이나 다중으로 여러 파일을 넘길 때 상태를 알 수 있다보니 아무래도 기다리는데, 도움이 될 것 같습니다.

 

바로 예제를 통해 확인해 보겠습니다.

 

xhr.upload.progress

사용법 ajax로 넘길때 xhr메소드를 추가하고 내부에 upload.onprogress메소드를 추가하여 정의하고자 하는 파일 내용을 추가하면 됩니다.

xhr: function(){
	var xhr = $.ajaxSettings.xhr();
	xhr.upload.onprogress = function(e){
		var per = e.loaded * 100 / e.total;
		console.log(per);
	};
	return xhr;
},

 

 

사용예제

fileupload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
	<title>Home</title>
</head>
<style>
	*{margin:0;padding:0}
	.progressContainer{position:relative;width: 450px;padding:20px 10px;border: 1px solid #eee;margin-top: 15px;background:#000;height:20px;}
	.progress{position:absolute;width: calc(100% - 20px);height: 20px;}
	.progressTotal{background: #5D5D5D;border-radius: 10px;}
	.progressNow{width: calc(0% - 20px);background: #FFF;border-radius: 10px;}
	.progressPer{background: transparent; text-align:center;color:#A6A6A6;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<body>
<h1>
	File Upload Test
</h1>

<P>  The time on the server is ${serverTime}. </P>
	<div style="width: 100%;padding: 25px;">
		<form id="fileForm" action="/upload.do" method="post" enctype="multipart/form-data">
			<input type="file" name="uploadFile" multiple>
			<button type="button" id="btn">전송</button>
		</form>
		<div class="progressContainer">
			<div class="progress progressTotal"></div>
			<div class="progress progressNow"></div>
			<div class="progress progressPer">0 %</div>
		</div>
	</div>
</body>
<script>
	$(function(){	
		$("#btn").on("click", function(){
			console.log("click Time : " + new Date);
			
			var form = $("#fileForm")[0];
			var formData = new FormData(form);
			$.ajax({
				type: "POST",
				enctype: 'multipart/form-data',
				url: "/upload.do",
				data: formData,
				processData: false,
				contentType: false,
				cache: false,
				xhr: function(){
					var xhr = $.ajaxSettings.xhr();
					xhr.upload.onprogress = function(e){
						var per = e.loaded * 100 / e.total;
						progressBar(per);
					};
					return xhr;
				},
				success: function (data) {
					console.log("SUCCESS : ", data);
				},
				error: function (e) {
					console.log("ERROR : ", e);
				}
			});
		});
	});
	
	function progressBar(per){
		if(per > 55){
			$(".progressPer").css("color", "#000");
		}
		per = per.toFixed(1);
		$(".progressPer").text(per+" %");
		$(".progressNow").css("width", "calc(" + per + "% - 20px)");
	}
</script>
</html>

btn이라는 버튼을 클릭하면 file에 존재하는 데이터를 넘기고 xhr메소드에 정의된 onprogress 메소드에 의해 결과 값을 노출 하는 예제입니다.

 

 

결과

극단적으로 퍼센트를 잘 보기위해 3천개의 텍스트파일을 업로드하는 테스트를 진행해봤습니다.

 


  1. draggable - div 드래그

    Date2021.03.31 Views273
    Read More
  2. easing - 효과의 진행 속도

    Date2021.03.26 Views156
    Read More
  3. fadeIn() , fadeOut() 을 이용한 간단한 자동 그림 전환

    Date2021.03.26 Views285
    Read More
  4. focus() 로 오브젝트 옮기기

    Date2021.03.26 Views174
    Read More
  5. foreach문

    Date2021.03.26 Views195
    Read More
  6. FORM 객체 초기화 하기

    Date2016.09.21 Views7349
    Read More
  7. getElementsByClassName 을 IE8 이하에서 동작하게 하는 코드

    Date2018.11.07 Views1300
    Read More
  8. getJSON() 함수

    Date2016.09.21 Views8379
    Read More
  9. getScript()

    Date2016.09.21 Views7125
    Read More
  10. Get방식 파라미터 전송 & 탭 메뉴 선택

    Date2016.09.21 Views8566
    Read More
  11. Hide pager if bxslider has only 1 slide

    Date2018.11.07 Views1305
    Read More
  12. hover 메소드를 이용해서 메뉴 on, off를 구현

    Date2016.09.21 Views8485
    Read More
  13. Html 색상표 모음, 색상코드표 (RGB), 이미지X, 복사 가능

    Date2016.11.17 Views8169
    Read More
  14. html 테이블 고정 (table fixed)

    Date2016.11.18 Views7120
    Read More
  15. iframe height auto resize

    Date2018.11.07 Views1419
    Read More
  16. input checkbox 모두 체크하기

    Date2021.03.26 Views176
    Read More
  17. Input TYPE=“File” 을 히든으로 하고 외부 버튼을 눌러서 파일을 선택

    Date2016.09.21 Views6808
    Read More
  18. javascript, jQuery에서 루프 돌리기 예 (for, forEach, each)

    Date2021.03.25 Views311
    Read More
  19. jqGrid / custom pager / custom paging / 그리드 / 페이징

    Date2016.09.09 Views12539
    Read More
  20. jQuery - ajax xhr을 활용한 파일 업로드 진행 상태 확인하기

    Date2021.03.09 Views599
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 Next
/ 9

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved