jQuery없이 네티브자바스크립트로 서버에 요청보내기5- 파일업로드
파일 업로드를 위해 필요한 <input type="file" id="test-input"> 을 작성합니다.
jQuery에서 파일업로드를 어떻게 하는지 먼저 보겠습니다.
[code]
var file = $('#test-input')[0].files[0],
formData = new FormData();
formData.append('file', file);
$.ajax('myserver/uploads', {
method: 'POST',
contentType: false,
processData: false,
data: formData
});
또는
var file = $('#test-input')[0].files[0];
$.ajax('myserver/uploads', {
method: 'POST',
contentType: file.type,
processData: false,
data: file
});
[/code]
XMLHttpRequest 기능을 이용한 파일업로드입니다.
[code]
var formData = new FormData(),
file = document.getElementById('test-input').files[0],
xhr = new XMLHttpRequest();
formData.append('file', file);
xhr.open('POST', 'myserver/uploads');
xhr.send(formData);
또는
var file = document.getElementById('test-input').files[0],
xhr = new XMLHttpRequest();
xhr.open('POST', 'myserver/uploads');
xhr.setRequestHeader('Content-Type', file.type);
xhr.send(file);
[/code]
보시는거처럼 jQuery의 파일업로드는 사실상 XMLHttpRequest를 래퍼한것입니다.
댓글 1개
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4976 | 기타 | 5년 전 | 2120 | ||
| 4975 | 웹서버 |
nooree
|
5년 전 | 2768 | |
| 4974 | node.js |
younhoso
|
5년 전 | 2135 | |
| 4973 | PHP |
|
5년 전 | 3639 | |
| 4972 | 기타 |
younhoso
|
6년 전 | 2262 | |
| 4971 | JavaScript | 6년 전 | 2488 | ||
| 4970 | 웹서버 | 6년 전 | 4123 | ||
| 4969 | JavaScript |
|
6년 전 | 2783 | |
| 4968 | JavaScript |
|
6년 전 | 1935 | |
| 4967 | JavaScript |
younhoso
|
6년 전 | 2043 | |
| 4966 | JavaScript |
|
6년 전 | 2116 | |
| 4965 | PHP |
|
6년 전 | 2104 | |
| 4964 | 기타 | 6년 전 | 2921 | ||
| 4963 | JavaScript | 6년 전 | 2090 | ||
| 4962 | JavaScript | 6년 전 | 2286 | ||
| 4961 | jQuery |
아이티몬스
|
6년 전 | 2094 | |
| 4960 | PHP | 6년 전 | 2739 | ||
| 4959 | 기타 | 6년 전 | 2566 | ||
| 4958 | PHP |
아이티몬스
|
6년 전 | 4577 | |
| 4957 | 기타 | 6년 전 | 2094 | ||
| 4956 | 정규표현식 |
하늘위의길
|
6년 전 | 2322 | |
| 4955 | 정규표현식 |
하늘위의길
|
6년 전 | 2675 | |
| 4954 | 웹서버 |
하늘위의길
|
6년 전 | 4138 | |
| 4953 | PHP | 6년 전 | 2525 | ||
| 4952 | PHP |
몰라무서워
|
6년 전 | 3212 | |
| 4951 | MySQL | 6년 전 | 4131 | ||
| 4950 | jQuery | 6년 전 | 3614 | ||
| 4949 | node.js | 6년 전 | 2681 | ||
| 4948 | node.js | 6년 전 | 2406 | ||
| 4947 | node.js | 6년 전 | 2647 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기