You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 5-network/02-formdata/article.md
+27-26Lines changed: 27 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
-
2
-
# FormData
1
+
2
+
# FormData 객체
3
3
4
4
이번 챕터에선 파일 여부나 추가 필드 여부 등과 상관없이 통용되는 HTML 폼(form) 전송 방법에 대해 알아보겠습니다.
5
5
6
-
[FormData](https://xhr.spec.whatwg.org/#interface-formdata) objects can help with that. As you might have guessed, it's the object to represent HTML form data.
6
+
[FormData](https://xhr.spec.whatwg.org/#interface-formdata)는 폼을 쉽게 보내도록 도와주는 객체입니다. 이름을 보고 유추하셨듯이 `FormData` 객체는 HTML 폼 데이터를 나타냅니다.
7
7
8
-
The constructor is:
8
+
생성자는 다음과 같습니다.
9
9
```js
10
10
let formData =newFormData([form]);
11
11
```
12
12
13
13
HTML에 `form` 요소가 있는 경우, 위와 같은 코드를 작성하면 해당 폼 요소의 필드 전체가 자동 반영됩니다.
14
14
15
-
The special thing about`FormData`is that network methods, such as `fetch`, can accept a `FormData` object as a body. It's encoded and sent out with`Content-Type: multipart/form-data`.
15
+
`fetch` 등의 네트워크 메서드가`FormData`객체를 바디로 받는다는 건 `FormData`의 특징입니다. 이때 브라우저가 보내는 HTTP 메시지는 인코딩되고`Content-Type` 속성은 `multipart/form-data`로 지정된 후 전송됩니다.
16
16
17
-
From the server point of view, that looks like a usual form submission.
17
+
서버 관점에선 `FormData`를 사용한 방식과 일반 폼 전송 방식에 차이가 없습니다
18
18
19
19
## 간단한 폼 전송하기
20
20
@@ -24,7 +24,7 @@ From the server point of view, that looks like a usual form submission.
24
24
25
25
```html run autorun
26
26
<formid="formElem">
27
-
<inputtype="text"name="name"value="John">
27
+
<inputtype="text"name="name"value="Bora">
28
28
<inputtype="text"name="surname"value="Lee">
29
29
<inputtype="submit">
30
30
</form>
@@ -53,20 +53,20 @@ From the server point of view, that looks like a usual form submission.
53
53
54
54
`FormData`에 속하는 필드는 아래와 같은 메서드로 수정할 수 있습니다.
55
55
56
-
-`formData.append(name, value)` - add a form field with the given `name` and `value`,
56
+
-`formData.append(name, value)` - `name`과 `value`를 가진 폼 필드를 추가
57
57
-`formData.append(name, blob, fileName)` - `<input type="file">`형태의 필드를 추가. 세 번째 인수 `fileName`은 (필드 이름이 아니고) 사용자가 해당 이름을 가진 파일을 폼에 추가한 것처럼 설정해줌
58
-
-`formData.delete(name)` - remove the field with the given `name`,
58
+
-`formData.delete(name)` - `name`에 해당하는 필드를 삭제
59
59
-`formData.get(name)` - `name`에 해당하는 필드의 값을 가져옴
60
-
-`formData.has(name)` - if there exists a field with the given `name`, returns `true`, otherwise `false`
60
+
-`formData.has(name)` - `name`에 해당하는 필드가 있으면 `true`를, 그렇지 않으면 `false`를 반환
61
61
62
-
A form is technically allowed to have many fields with the same `name`, so multiple calls to `append` add more same-named fields.
62
+
폼은 이름(`name`)이 같은 필드 여러 개를 허용하기 때문에 `append` 메서드를 여러 번 호출해 이름이 같은 필드를 계속 추가해도 문제가 없습니다.
63
63
64
64
`append` 메서드 이외에 필드 추가 시 사용할 수 있는 메서드로 `set`도 있습니다. `set`이 `append` 메서드와 다른 점은 `set`은 `name`과 동일한 이름을 가진 필드를 모두 제거하고 새로운 필드 하나를 추가한다는 데 있습니다. 따라서 `set` 메서드를 쓰면 `name`을 가진 필드가 단 한 개만 있게끔 보장할 수 있습니다. 이 외에 다른 기능은 `append` 메서드와 동일합니다.
65
65
66
-
-`formData.set(name, value)`,
67
-
-`formData.set(name, blob, fileName)`.
66
+
-`formData.set(name, value)`
67
+
-`formData.set(name, blob, fileName)`
68
68
69
-
Also we can iterate over formData fields using `for..of`loop:
The form is always sent as `Content-Type: multipart/form-data`, this encoding allows to send files. So, `<input type="file">` fields are sent also, similar to a usual form submission.
84
+
폼을 전송할 때 HTTP 메시지의 `Content-Type` 속성은 항상 `multipart/form-data`이고 메시지는 인코딩되어 전송됩니다. 파일이 있는 폼도 당연히 이 규칙을 따르기 때문에 `<input type="file">`로 지정한 필드 역시 일반 폼을 전송할 때와 유사하게 전송됩니다.
@@ -112,11 +112,11 @@ The form is always sent as `Content-Type: multipart/form-data`, this encoding al
112
112
113
113
## Blob 데이터가 있는 폼 전송하기
114
114
115
-
<info:fetch> 챕터에서 살펴본 바와 같이 이미지 같은 동적으로 생성된 바이너리 파일은 `Blob` 객체를 사용해 쉽게 전송할 수 있습니다. 이때 `Blob` 객체는 `fetch` 메서드의 `body` 매개변수에 바로 넘겨줄 수 있죠.
115
+
<info:fetch> 챕터에서 살펴본 바와 같이 이미지 같은 동적으로 생성된 바이너리 파일은 `Blob` 객체를 사용해 쉽게 전송할 수 있습니다. 이때 `Blob` 객체는 `fetch` 메서드의 `body` 매개변수에 바로 넘겨줄 수 있죠.
116
116
117
-
In practice though, it's often convenient to send an image not separately, but as a part of the form, with additional fields, such as "name" and other metadata.
117
+
그런데 실제 코딩을 하다 보면 이미지를 별도로 넘겨주는 것보다 폼에 필드를 추가하고 여기에 이미지 '이름' 등의 메타데이터를 같이 실어 넘겨주는 게 좀 더 편리합니다.
118
118
119
-
서버 입장에서도 원시 바이너리 데이터를 받는 것보다 multipart-encoded 폼을 받는게 좀 더 적합하죠.
119
+
서버 입장에서도 원시 바이너리 데이터를 받는 것보다 multipart-encoded 폼을 받는 게 좀 더 적합하죠.
120
120
121
121
아래는 `<canvas>`를 사용해 만든 이미지를 `FormData`를 사용해 폼 형태로 다른 추가 필드와 함께 전송하는 예시입니다.
122
122
@@ -154,17 +154,17 @@ In practice though, it's often convenient to send an image not separately, but a
154
154
</body>
155
155
```
156
156
157
-
Please note how the image `Blob` is added:
157
+
예시에서 이미지 `Blob`을 추가한 코드를 다시 봅시다.
158
158
159
159
```js
160
160
formData.append("image", imageBlob, "image.png");
161
161
```
162
162
163
163
이 코드는 폼에 `<input type="file" name="image">` 태그가 있고, 사용자 기기의 파일 시스템에서 파일명이 `"image.png"`(3번째 인수 참고)인 `imageBlob` 데이터(2번째 인수 참고)를 추가한 것과 동일한 효과를 줍니다.
164
164
165
-
The server reads form data and the file, as if it were a regular form submission.
165
+
요청을 받은 서버는 일반 폼과 동일하게 폼 데이터와 파일을 읽고 처리합니다.
166
166
167
-
## Summary
167
+
## 요약
168
168
169
169
[FormData](https://xhr.spec.whatwg.org/#interface-formdata) 객체는 `fetch` 등의 네트워크 메서드를 통해 HTML 폼을 보내는데 사용됩니다.
170
170
@@ -177,9 +177,10 @@ The server reads form data and the file, as if it were a regular form submission
177
177
178
178
메서드를 사용할 때 주의할 점 2가지가 있습니다.
179
179
180
-
1.The `set`method removes fields with the same name, `append` doesn't. That's the only difference between them.
180
+
1.`set`메서드는 `name`이 같은 필드 모두를 지우고 `append`는 그렇지 않습니다. 다른 차이는 없습니다.
181
181
2. 파일을 보낼 땐 세 번째 인수가 필요한데 이 인수는 사용자 파일 시스템에서 지정한 파일명과 동일하게 지정됩니다.
0 commit comments