Skip to content

Commit ed656e5

Browse files
[FormData 객체] 번역
1 parent a5d4e37 commit ed656e5

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

5-network/02-formdata/article.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11

2-
# FormData
2+
# FormData 객체
33

4-
This chapter is about sending HTML forms: with or without files, with additional fields and so on.
4+
이번 챕터에선 파일 여부나 추가 필드 여부 등과 상관없이 통용되는 HTML 폼(form) 전송 방법에 대해 알아보겠습니다.
55

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 폼 데이터를 나타냅니다.
77

8-
The constructor is:
8+
생성자는 다음과 같습니다.
99
```js
1010
let formData = new FormData([form]);
1111
```
1212

13-
If HTML `form` element is provided, it automatically captures its fields.
13+
HTML에 `form` 요소가 있는 경우, 위와 같은 코드를 작성하면 해당 폼 요소의 필드 전체가 자동 반영됩니다.
1414

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`로 지정된 후 전송됩니다.
1616

17-
From the server point of view, that looks like a usual form submission.
17+
서버 관점에선 `FormData`를 사용한 방식과 일반 폼 전송 방식에 차이가 없습니다
1818

19-
## Sending a simple form
19+
## 간단한 폼 전송하기
2020

21-
Let's send a simple form first.
21+
아주 간단한 폼을 전송한다고 가정해봅시다.
2222

23-
As you can see, that's almost one-liner:
23+
보시다시피 아주 짧은 코드로도 전송 처리가 가능합니다.
2424

2525
```html run autorun
2626
<form id="formElem">
27-
<input type="text" name="name" value="John">
28-
<input type="text" name="surname" value="Smith">
27+
<input type="text" name="name" value="Bora">
28+
<input type="text" name="surname" value="Lee">
2929
<input type="submit">
3030
</form>
3131

@@ -47,47 +47,47 @@ As you can see, that's almost one-liner:
4747
</script>
4848
```
4949

50-
In this example, the server code is not presented, as it's beyound our scope. The server accepts the POST request and replies "User saved".
50+
요청을 받아 처리하는 서버 측 코드는 튜토리얼 범위를 넘어서서 추가하진 않았는데, 서버는 POST 요청을 받아 '저장 성공'이라는 응답을 보내준다고 정도만 알고 계시면 됩니다.
5151

52-
## FormData Methods
52+
## FormData 메서드
5353

54-
We can modify fields in `FormData` with methods:
54+
`FormData`에 속하는 필드는 아래와 같은 메서드로 수정할 수 있습니다.
5555

56-
- `formData.append(name, value)` - add a form field with the given `name` and `value`,
57-
- `formData.append(name, blob, fileName)` - add a field as if it were `<input type="file">`, the third argument `fileName` sets file name (not form field name), as it were a name of the file in user's filesystem,
58-
- `formData.delete(name)` - remove the field with the given `name`,
59-
- `formData.get(name)` - get the value of the field with the given `name`,
60-
- `formData.has(name)` - if there exists a field with the given `name`, returns `true`, otherwise `false`
56+
- `formData.append(name, value)` - `name``value`를 가진 폼 필드를 추가
57+
- `formData.append(name, blob, fileName)` - `<input type="file">`형태의 필드를 추가. 세 번째 인수 `fileName`은 (필드 이름이 아니고) 사용자가 해당 이름을 가진 파일을 폼에 추가한 것처럼 설정해줌
58+
- `formData.delete(name)` - `name`에 해당하는 필드를 삭제
59+
- `formData.get(name)` - `name`에 해당하는 필드의 값을 가져옴
60+
- `formData.has(name)` - `name`에 해당하는 필드가 있으면 `true`를, 그렇지 않으면 `false`를 반환
6161

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` 메서드를 여러 번 호출해 이름이 같은 필드를 계속 추가해도 문제가 없습니다.
6363

64-
There's also method `set`, with the same syntax as `append`. The difference is that `.set` removes all fields with the given `name`, and then appends a new field. So it makes sure there's only one field with such `name`, the rest is just like `append`:
64+
`append` 메서드 이외에 필드 추가 시 사용할 수 있는 메서드로 `set`도 있습니다. `set``append` 메서드와 다른 점은 `set``name`과 동일한 이름을 가진 필드를 모두 제거하고 새로운 필드 하나를 추가한다는 데 있습니다. 따라서 `set` 메서드를 쓰면 `name`을 가진 필드가 단 한 개만 있게끔 보장할 수 있습니다. 이 외에 다른 기능은 `append` 메서드와 동일합니다.
6565

66-
- `formData.set(name, value)`,
67-
- `formData.set(name, blob, fileName)`.
66+
- `formData.set(name, value)`
67+
- `formData.set(name, blob, fileName)`
6868

69-
Also we can iterate over formData fields using `for..of` loop:
69+
참고로 폼 데이터 필드에 반복 작업을 할땐 `for..of` 루프를 사용할 수 있습니다.
7070

7171
```js run
7272
let formData = new FormData();
7373
formData.append('key1', 'value1');
7474
formData.append('key2', 'value2');
7575

76-
// List key/value pairs
76+
// key/value 쌍이 담긴 리스트
7777
for(let [name, value] of formData) {
78-
alert(`${name} = ${value}`); // key1=value1, then key2=value2
78+
alert(`${name} = ${value}`); // key1 = value1, then key2 = value2
7979
}
8080
```
8181

82-
## Sending a form with a file
82+
## 파일이 있는 폼 전송하기
8383

84-
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">`로 지정한 필드 역시 일반 폼을 전송할 때와 유사하게 전송됩니다.
8585

86-
Here's an example with such form:
86+
파일이 있는 폼 예시를 살펴봅시다.
8787

8888
```html run autorun
8989
<form id="formElem">
90-
<input type="text" name="firstName" value="John">
90+
<input type="text" name="firstName" value="Bora">
9191
Picture: <input type="file" name="picture" accept="image/*">
9292
<input type="submit">
9393
</form>
@@ -110,21 +110,21 @@ Here's an example with such form:
110110
</script>
111111
```
112112

113-
## Sending a form with Blob data
113+
## Blob 데이터가 있는 폼 전송하기
114114

115-
As we've seen in the chapter <info:fetch>, it's easy to send dynamically generated binary data e.g. an image, as `Blob`. We can supply it directly as `fetch` parameter `body`.
115+
<info:fetch> 챕터에서 살펴본 바와 같이 이미지 같은 동적으로 생성된 바이너리 파일은 `Blob` 객체를 사용해 쉽게 전송할 수 있습니다. 이때 `Blob` 객체는 `fetch` 메서드의 `body` 매개변수에 바로 넘겨줄 수 있죠.
116116

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+
그런데 실제 코딩을 하다 보면 이미지를 별도로 넘겨주는 것보다 폼에 필드를 추가하고 여기에 이미지 '이름' 등의 메타데이터를 같이 실어 넘겨주는 게 좀 더 편리합니다.
118118

119-
Also, servers are usually more suited to accept multipart-encoded forms, rather than raw binary data.
119+
서버 입장에서도 원시 바이너리 데이터를 받는 것보다 multipart-encoded 폼을 받는 게 좀 더 적합하죠.
120120

121-
This example submits an image from `<canvas>`, along with some other fields, as a form, using `FormData`:
121+
아래는 `<canvas>`를 사용해 만든 이미지를 `FormData`를 사용해 폼 형태로 다른 추가 필드와 함께 전송하는 예시입니다.
122122

123123
```html run autorun height="90"
124124
<body style="margin:0">
125125
<canvas id="canvasElem" width="100" height="80" style="border:1px solid"></canvas>
126126

127-
<input type="button" value="Submit" onclick="submit()">
127+
<input type="button" value="이미지 전송" onclick="submit()">
128128

129129
<script>
130130
canvasElem.onmousemove = function(e) {
@@ -138,7 +138,7 @@ This example submits an image from `<canvas>`, along with some other fields, as
138138
139139
*!*
140140
let formData = new FormData();
141-
formData.append("firstName", "John");
141+
formData.append("firstName", "Bora");
142142
formData.append("image", imageBlob, "image.png");
143143
*/!*
144144
@@ -154,36 +154,36 @@ This example submits an image from `<canvas>`, along with some other fields, as
154154
</body>
155155
```
156156

157-
Please note how the image `Blob` is added:
157+
예시에서 이미지 `Blob`을 추가한 코드를 다시 봅시다.
158158

159159
```js
160160
formData.append("image", imageBlob, "image.png");
161161
```
162162

163-
That's same as if there were `<input type="file" name="image">` in the form, and the visitor submitted a file named `"image.png"` (3rd argument) with the data `imageBlob` (2nd argument) from their filesystem.
163+
이 코드는 폼에 `<input type="file" name="image">` 태그가 있고, 사용자 기기의 파일 시스템에서 파일명이 `"image.png"`(3번째 인수 참고)인 `imageBlob` 데이터(2번째 인수 참고)를 추가한 것과 동일한 효과를 줍니다.
164164

165-
The server reads form data and the file, as if it were a regular form submission.
165+
요청을 받은 서버는 일반 폼과 동일하게 폼 데이터와 파일을 읽고 처리합니다.
166166

167-
## Summary
167+
## 요약
168168

169-
[FormData](https://xhr.spec.whatwg.org/#interface-formdata) objects are used to capture HTML form and submit it using `fetch` or another network method.
169+
[FormData](https://xhr.spec.whatwg.org/#interface-formdata) 객체는 `fetch` 등의 네트워크 메서드를 통해 HTML 폼을 보내는데 사용됩니다.
170170

171-
We can either create `new FormData(form)` from an HTML form, or create a object without a form at all, and then append fields with methods:
171+
`FormData` 객체는 HTML 폼(`form`)을 직접 넘겨 `new FormData(form)`으로 만들 수도 있고, HTML 폼 없이 다음과 같은 메서드로 필드를 추가해 만들 수도 있습니다.
172172

173173
- `formData.append(name, value)`
174174
- `formData.append(name, blob, fileName)`
175175
- `formData.set(name, value)`
176176
- `formData.set(name, blob, fileName)`
177177

178-
Let's note two peculiarities here:
178+
메서드를 사용할 때 주의할 점 2가지가 있습니다.
179179

180-
1. The `set` method removes fields with the same name, `append` doesn't. That's the only difference between them.
181-
2. To send a file, 3-argument syntax is needed, the last argument is a file name, that normally is taken from user filesystem for `<input type="file">`.
180+
1. `set` 메서드는 `name`이 같은 필드 모두를 지우고 `append`는 그렇지 않습니다. 다른 차이는 없습니다.
181+
2. 파일을 보낼 땐 세 번째 인수가 필요한데 이 인수는 사용자 파일 시스템에서 지정한 파일명과 동일하게 지정됩니다.
182182

183-
Other methods are:
183+
이외에도 다음과 같은 메서드가 있습니다
184184

185185
- `formData.delete(name)`
186186
- `formData.get(name)`
187187
- `formData.has(name)`
188188

189-
That's it!
189+
다룰 내용은 여기까지입니다!

0 commit comments

Comments
 (0)