1
00:00:02,060 --> 00:00:03,540
And actually we're going to start

2
00:00:03,540 --> 00:00:06,993
on this add a new user page here, this page.

3
00:00:07,870 --> 00:00:10,320
There I get a very simple form,

4
00:00:10,320 --> 00:00:14,130
it's this new user EJS file which is responsible for it.

5
00:00:14,130 --> 00:00:18,490
And there I got one input where I want a username,

6
00:00:18,490 --> 00:00:20,630
so some simple text input.

7
00:00:20,630 --> 00:00:24,910
And then I got this part here, where I want a user image,

8
00:00:24,910 --> 00:00:26,580
and I got these question marks here

9
00:00:26,580 --> 00:00:29,640
to signal that I will need to add something there,

10
00:00:29,640 --> 00:00:32,320
because at the moment we've got no way of picking an image,

11
00:00:32,320 --> 00:00:34,840
we can just click this button, but of course right now,

12
00:00:34,840 --> 00:00:37,363
we're not yet ready to submit this form.

13
00:00:38,816 --> 00:00:42,160
So how can we now ensure that a user

14
00:00:42,160 --> 00:00:44,093
is able to select an image?

15
00:00:45,210 --> 00:00:48,670
Well, thankfully that's a core functionality,

16
00:00:48,670 --> 00:00:50,170
you are not the first one

17
00:00:50,170 --> 00:00:52,690
who wants to add such a feature to a website.

18
00:00:52,690 --> 00:00:56,530
And therefore, thankfully, there is an HTML element

19
00:00:56,530 --> 00:00:59,870
that allows us to add a file picker.

20
00:00:59,870 --> 00:01:03,800
And that indeed actually is the input element again,

21
00:01:03,800 --> 00:01:05,830
which we already used up here,

22
00:01:05,830 --> 00:01:08,483
and which we saw a lot throughout the course already.

23
00:01:09,460 --> 00:01:12,270
But here we now don't want type text,

24
00:01:12,270 --> 00:01:16,000
but instead there is type file, which you can set.

25
00:01:16,000 --> 00:01:17,900
That's one of the built in types,

26
00:01:17,900 --> 00:01:20,150
which is supported by all browsers,

27
00:01:20,150 --> 00:01:22,600
and this will do what the name suggests.

28
00:01:22,600 --> 00:01:24,470
It will in the end give us an input

29
00:01:24,470 --> 00:01:27,053
that allows us to select a file.

30
00:01:28,420 --> 00:01:32,260
Now we can give this an ID, image and a name,

31
00:01:32,260 --> 00:01:33,893
like, image again.

32
00:01:35,536 --> 00:01:39,760
And if we now save that and we then reload this form,

33
00:01:39,760 --> 00:01:42,050
we got this file picker here,

34
00:01:42,050 --> 00:01:45,523
which gives us this choose file button here, this option.

35
00:01:46,850 --> 00:01:48,703
Now we can click this button,

36
00:01:50,055 --> 00:01:54,280
and it will open this file picker for us here,

37
00:01:54,280 --> 00:01:56,710
and you can then navigate around on your system

38
00:01:56,710 --> 00:01:59,910
to select the folder where the file lives,

39
00:01:59,910 --> 00:02:01,610
that you wanna upload.

40
00:02:01,610 --> 00:02:05,820
And here I prepared a simple file, an image of myself here,

41
00:02:05,820 --> 00:02:08,630
and you can of course, try this with any image.

42
00:02:08,630 --> 00:02:10,120
It should be an image here

43
00:02:10,120 --> 00:02:12,650
to proceed with the rest of this section,

44
00:02:12,650 --> 00:02:14,660
because I will later also show you

45
00:02:14,660 --> 00:02:18,250
how to preview this image, but theoretically of course,

46
00:02:18,250 --> 00:02:19,813
you could pick any file.

47
00:02:20,700 --> 00:02:24,093
But here I'm going to go with an image and I can click open.

48
00:02:24,960 --> 00:02:26,670
Now we don't see a preview here.

49
00:02:26,670 --> 00:02:28,520
We don't see a preview of the image,

50
00:02:28,520 --> 00:02:32,040
that is something we would have to build on our own.

51
00:02:32,040 --> 00:02:34,180
And it is something we will build on our own

52
00:02:34,180 --> 00:02:36,910
throughout this section, but to get started,

53
00:02:36,910 --> 00:02:39,120
to keep things simple right now,

54
00:02:39,120 --> 00:02:42,150
we will just leave it like this.

55
00:02:42,150 --> 00:02:45,120
And now we've got this file added here,

56
00:02:45,120 --> 00:02:47,280
now we could enter a username as well,

57
00:02:47,280 --> 00:02:49,110
and then submit this form.

58
00:02:49,110 --> 00:02:53,310
But, before we do that there are a couple of other things

59
00:02:53,310 --> 00:02:56,670
we might wanna do, and at least one other thing

60
00:02:56,670 --> 00:02:57,943
we have to do.

61
00:02:58,790 --> 00:03:01,890
Now let's start with the things we might wanna do.

62
00:03:01,890 --> 00:03:03,350
Besides showing a preview,

63
00:03:03,350 --> 00:03:05,460
which is something we'll explore later,

64
00:03:05,460 --> 00:03:08,940
we of course might wanna configure this file input

65
00:03:08,940 --> 00:03:09,910
a little bit more,

66
00:03:09,910 --> 00:03:14,570
to control which kind of files are selected here.

67
00:03:14,570 --> 00:03:16,750
For example, we can again add required,

68
00:03:16,750 --> 00:03:19,920
to ensure that we must have some file.

69
00:03:19,920 --> 00:03:23,710
If I set required here and I reload this page,

70
00:03:23,710 --> 00:03:27,620
and I enter a username here, but then I don't choose a file,

71
00:03:27,620 --> 00:03:29,180
I get a warning here,

72
00:03:29,180 --> 00:03:32,010
so that might be something we might wanna do.

73
00:03:32,010 --> 00:03:35,990
In addition, it's very likely that we don't wanna accept

74
00:03:35,990 --> 00:03:40,240
all kinds of files, but that, for example here,

75
00:03:40,240 --> 00:03:42,040
we want an image.

76
00:03:42,040 --> 00:03:45,250
So therefore, thankfully with the file input,

77
00:03:45,250 --> 00:03:47,840
we can also restrict the types of files

78
00:03:47,840 --> 00:03:49,483
that can be selected here.

79
00:03:50,540 --> 00:03:52,280
For this, to achieve this,

80
00:03:52,280 --> 00:03:55,510
we can add an accept attribute here,

81
00:03:55,510 --> 00:03:58,640
and then there you can simply list the file extensions

82
00:03:58,640 --> 00:04:02,210
that you wanna allow, for example, .JPG.

83
00:04:02,210 --> 00:04:04,660
And then this is a comma separated list,

84
00:04:04,660 --> 00:04:08,069
you can add all the files you do want to allow,

85
00:04:08,069 --> 00:04:11,015
like also JPEG with an E.

86
00:04:11,015 --> 00:04:13,930
Now we can only select such files.

87
00:04:13,930 --> 00:04:18,930
And to prove this, if I temporarily set this to only PNG,

88
00:04:18,990 --> 00:04:23,190
and save that, and we then go back and reload.

89
00:04:23,190 --> 00:04:27,763
If I click choose file, I can't select this JPEG file here.

90
00:04:28,886 --> 00:04:30,603
So that's how you can control

91
00:04:30,603 --> 00:04:33,720
which files can be selected here.

92
00:04:33,720 --> 00:04:36,360
Now there are different ways of writing this.

93
00:04:36,360 --> 00:04:38,500
You can list those extensions,

94
00:04:38,500 --> 00:04:40,890
the dot is important in that case.

95
00:04:40,890 --> 00:04:44,513
You can also write something like image, slash JPEG,

96
00:04:45,420 --> 00:04:47,480
to allow all JPEG images,

97
00:04:47,480 --> 00:04:50,770
no matter if the file ends with JPEG with an E,

98
00:04:50,770 --> 00:04:51,963
or without an E.

99
00:04:52,810 --> 00:04:56,113
And you can then also still combine that like this,

100
00:04:56,970 --> 00:05:01,480
or you can type image, slash star to allow all images,

101
00:05:01,480 --> 00:05:04,053
but for example, no PDF documents.

102
00:05:05,730 --> 00:05:08,530
Here I will write image, slash JPEG,

103
00:05:08,530 --> 00:05:13,343
comma image, slash PNG, to allow JPEG and PNG files.

104
00:05:16,110 --> 00:05:20,143
With that if I reload, I can select my JPEG file here,

105
00:05:20,980 --> 00:05:25,163
and I can also select that if it ends with JPEG with an E.

106
00:05:26,480 --> 00:05:28,680
So now this can be selected,

107
00:05:28,680 --> 00:05:31,743
and we now do have that file in this form.

108
00:05:32,740 --> 00:05:34,980
Again, a preview might be nice to have,

109
00:05:34,980 --> 00:05:36,800
but that's a little bit more advanced,

110
00:05:36,800 --> 00:05:39,830
hence I'll dive into this later, and then for instead now,

111
00:05:39,830 --> 00:05:43,040
I wanna focus on submitting this form,

112
00:05:43,040 --> 00:05:44,980
on the client side for now.

113
00:05:44,980 --> 00:05:46,390
Handling it on the back end

114
00:05:46,390 --> 00:05:48,363
will be the next step thereafter.

115
00:05:49,250 --> 00:05:53,520
And that is the mandatory step, which is not optional.

116
00:05:53,520 --> 00:05:56,120
You might wanna control what gets selected here,

117
00:05:56,120 --> 00:05:57,990
you definitely have to control

118
00:05:57,990 --> 00:06:00,803
how this form is submitted, though.

119
00:06:00,803 --> 00:06:04,130
Right now it's configured as a default form,

120
00:06:04,130 --> 00:06:06,993
with some path and this method.

121
00:06:07,930 --> 00:06:10,320
Now this worked up to this point in the course,

122
00:06:10,320 --> 00:06:13,610
but as soon as your form includes files,

123
00:06:13,610 --> 00:06:17,690
you have to add an extra attribute to your form.

124
00:06:17,690 --> 00:06:20,883
And that's the enc type attribute.

125
00:06:20,883 --> 00:06:25,290
This controls how the form is encoded,

126
00:06:25,290 --> 00:06:29,530
so how that data, which is part of this page,

127
00:06:29,530 --> 00:06:33,370
will be packed into the request body by the browser,

128
00:06:33,370 --> 00:06:35,540
when the browser sends that request,

129
00:06:35,540 --> 00:06:37,730
with that data to the server.

130
00:06:37,730 --> 00:06:40,570
So the browser will do the heavy lifting,

131
00:06:40,570 --> 00:06:43,403
but you have to tell the browser how it should be done.

132
00:06:44,660 --> 00:06:48,820
Now the default mode is, application/x-www-form-urlencoded,

133
00:06:52,680 --> 00:06:54,930
which sounds super weird and fancy,

134
00:06:54,930 --> 00:06:58,050
but that's just a default way of submitting forms,

135
00:06:58,050 --> 00:07:01,310
which we automatically used thus far.

136
00:07:01,310 --> 00:07:04,443
Since it's the default, we didn't have to specify it.

137
00:07:05,880 --> 00:07:10,550
But now we wanna use multipart/form-data.

138
00:07:10,550 --> 00:07:13,750
That's the encoding type you have to choose,

139
00:07:13,750 --> 00:07:18,750
if you have a form that includes at least one file picker.

140
00:07:19,080 --> 00:07:22,150
If you have no file picker, you could also use it,

141
00:07:22,150 --> 00:07:26,780
but then the other mode, the default mode is recommended.

142
00:07:26,780 --> 00:07:28,690
But if you have at least one file,

143
00:07:28,690 --> 00:07:32,480
you must switch to enc type, multi-part form data,

144
00:07:32,480 --> 00:07:36,713
for that file to be submitted to the server successfully.

145
00:07:38,010 --> 00:07:41,170
And therefore that was a mandatory step.

146
00:07:41,170 --> 00:07:43,190
With that, if we now save this,

147
00:07:43,190 --> 00:07:45,480
we would be able to submit this form,

148
00:07:45,480 --> 00:07:47,853
as soon as we also enter a username.

149
00:07:48,687 --> 00:07:50,940
But before we can do that,

150
00:07:50,940 --> 00:07:53,830
we now of course have to move to the server side,

151
00:07:53,830 --> 00:07:57,270
to the backend, and write some code there

152
00:07:57,270 --> 00:07:59,420
to handle this incoming request,

153
00:07:59,420 --> 00:08:03,313
and most importantly, to also handle the incoming file.

