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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 5126 | PHP |
|
3년 전 | 1797 | |
| 5125 | PHP | 3년 전 | 1598 | ||
| 5124 | PHP | 3년 전 | 2115 | ||
| 5123 | OS | 3년 전 | 1396 | ||
| 5122 | OS | 3년 전 | 1313 | ||
| 5121 | OS | 3년 전 | 1418 | ||
| 5120 | OS | 3년 전 | 1296 | ||
| 5119 | PHP |
|
3년 전 | 1143 | |
| 5118 | PHP | 3년 전 | 1352 | ||
| 5117 | Mobile |
|
3년 전 | 1397 | |
| 5116 | PHP | 3년 전 | 2291 | ||
| 5115 | MySQL |
welcome
|
3년 전 | 2903 | |
| 5114 | OS | 3년 전 | 2002 | ||
| 5113 | JavaScript | 3년 전 | 1539 | ||
| 5112 | PHP | 3년 전 | 1556 | ||
| 5111 | 기타 |
|
3년 전 | 1298 | |
| 5110 | PHP | 3년 전 | 4545 | ||
| 5109 | PHP | 3년 전 | 1578 | ||
| 5108 | 기타 |
|
3년 전 | 12174 | |
| 5107 | 기타 |
|
3년 전 | 2408 | |
| 5106 | 기타 |
|
3년 전 | 6321 | |
| 5105 | 기타 |
|
3년 전 | 3694 | |
| 5104 | 기타 |
|
3년 전 | 1501 | |
| 5103 | JavaScript | 3년 전 | 1690 | ||
| 5102 | 기타 |
|
3년 전 | 1269 | |
| 5101 | 기타 |
|
3년 전 | 1465 | |
| 5100 | 기타 |
|
3년 전 | 1305 | |
| 5099 | 웹서버 | 3년 전 | 2171 | ||
| 5098 | PHP | 3년 전 | 1326 | ||
| 5097 | PHP |
|
3년 전 | 1986 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기