1 00:00:00,300 --> 00:00:02,800 I have friends in this video. 2 00:00:03,240 --> 00:00:09,350 We shall see how a file uploading operation is done in a flask application. 3 00:00:14,410 --> 00:00:27,540 The applauding activity requires a very specific configuration of the ascherman for the form in return 4 00:00:27,550 --> 00:00:28,400 investment. 5 00:00:29,170 --> 00:00:32,320 There are different attributes that can be set to perform. 6 00:00:32,340 --> 00:00:37,610 For example, we have already seen that there is a post attribute, then there is an action attribute. 7 00:00:38,350 --> 00:00:45,680 There are two more attributes which must be specifically said in order to perform the upload operation. 8 00:00:46,330 --> 00:00:50,800 One of the attributes is the IANSITI or encryption type. 9 00:00:51,820 --> 00:00:59,440 It must be said to multiplatform data that is the prerequisite. 10 00:01:00,970 --> 00:01:04,180 And secondly, the matter must be placed. 11 00:01:05,560 --> 00:01:15,040 That means what the form would get as the method cannot be used for uploading a particular file. 12 00:01:15,640 --> 00:01:23,530 So in order to upload the file, the material must be set to post and the IANSITI must be set to multiplatform 13 00:01:23,530 --> 00:01:23,870 data. 14 00:01:24,490 --> 00:01:26,150 These two are mandatory. 15 00:01:27,160 --> 00:01:31,990 Next, the inside of the form. 16 00:01:32,200 --> 00:01:34,510 Then there should be air input. 17 00:01:34,510 --> 00:01:35,800 Time is equal to file. 18 00:01:37,030 --> 00:01:44,710 That input type is equal to file will provide the user with a button to browse this file system so that 19 00:01:44,710 --> 00:01:49,140 if you click on that button, he can select any file from the entire file system. 20 00:01:49,150 --> 00:01:55,240 You can go to a particular driver folder or wherever, select a file and then there will be a submit 21 00:01:55,240 --> 00:02:02,300 button, which if it is clicked, then the data will be posted using the post method to provide you 22 00:02:02,470 --> 00:02:03,070 specific. 23 00:02:03,070 --> 00:02:04,950 You are all that will be set as an action. 24 00:02:06,820 --> 00:02:11,500 Now vendor up the submit button is clicked by the user. 25 00:02:12,160 --> 00:02:20,640 The controller will go to the specify that you are in and the uploaded file will be transported or sealed 26 00:02:20,650 --> 00:02:22,660 in a temporary location on the server. 27 00:02:24,730 --> 00:02:31,260 It is for sale in the temporary location and then it is sold to its ultimate or final destination. 28 00:02:32,930 --> 00:02:37,730 Now, the name of the file with which it is to be sealed can be hard coded. 29 00:02:38,060 --> 00:02:46,400 You can give it in the form of a quotation on a string, but it is always recommended to obtain the 30 00:02:46,400 --> 00:02:53,750 name from a request files, file, object, file, name, property of the request, files, file object 31 00:02:53,990 --> 00:02:59,780 so that the same name that was present on the client's computer will be used as the name of the file 32 00:02:59,780 --> 00:03:00,050 to be. 33 00:03:00,590 --> 00:03:07,610 After uploading the file on the server, the name of their destination should be obtained from the request 34 00:03:07,610 --> 00:03:08,650 object itself. 35 00:03:12,390 --> 00:03:20,850 The recommended way, of course, is to obtain the secure version of the filename with the help of the 36 00:03:20,850 --> 00:03:30,990 secure filename function and the secure filename function is defined in the work module that any way 37 00:03:31,050 --> 00:03:37,320 will be installed or will be available when you import when you install the module itself. 38 00:03:37,650 --> 00:03:43,110 So this line must be present at the top of your application for along with the other import that may 39 00:03:43,110 --> 00:03:47,900 be necessary, define the part of the default upload folder. 40 00:03:49,620 --> 00:03:57,810 The default upload folder can be preserved in the application configuration by the by the use of AP, 41 00:03:57,810 --> 00:04:01,860 not config upload folder by default. 42 00:04:01,860 --> 00:04:07,630 It is the root folder of the application, but you can send any other in a folder that you may want. 43 00:04:08,190 --> 00:04:12,150 You can also set the maximum contained part. 44 00:04:12,570 --> 00:04:17,390 That means what the maximum size of the file to be uploaded can also be set. 45 00:04:17,400 --> 00:04:21,210 It is in the the parameter is to be provided in the bytes. 46 00:04:21,570 --> 00:04:27,480 So these are all the parameters of all the specifications that you must take into account. 47 00:04:32,130 --> 00:04:40,020 Now, this is the crux of the entire matter to be brought in or to be done in that in our practical 48 00:04:40,020 --> 00:04:49,530 exercise, what the form this form must be provided in the e-mail template that you may render with 49 00:04:49,530 --> 00:04:56,220 the help of insert a new function in which action is equal to where the actual uploading activity will 50 00:04:56,220 --> 00:04:56,520 be done. 51 00:04:56,550 --> 00:04:59,640 This is the real map to a particular view. 52 00:04:59,640 --> 00:05:07,500 Function metrological to post is also necessary, but more importantly, IANSITI is equal to multiplatform 53 00:05:07,500 --> 00:05:08,800 data that must be provided. 54 00:05:10,350 --> 00:05:17,400 OK, then in order to let the user browse his computer and select the file, you have to use input type 55 00:05:17,410 --> 00:05:19,590 equal to file and. 56 00:05:20,770 --> 00:05:26,440 The other button that will present on the form will be submitted see that there are two buttons on the 57 00:05:26,440 --> 00:05:26,800 form. 58 00:05:27,340 --> 00:05:30,460 One is the browser button and there is the submit button. 59 00:05:31,090 --> 00:05:35,650 Precisely for this purpose, the RNC type must be set to multipart. 60 00:05:36,250 --> 00:05:38,020 So such a form will be rendered. 61 00:05:39,400 --> 00:05:46,030 User will select a file, upload, submit the click the submit button. 62 00:05:46,030 --> 00:05:52,840 The file data will be sent to the upload function inside the view function mapping with this uploader 63 00:05:52,840 --> 00:06:01,420 you until you are to do this, find out the name of the file request on file file and then they have 64 00:06:01,420 --> 00:06:07,780 the final say on the server with the help of the filename that is extracted from the request of you. 65 00:06:08,200 --> 00:06:09,370 So this is the directory. 66 00:06:09,370 --> 00:06:12,360 Did you see that there are three or four different activities to be performed. 67 00:06:12,970 --> 00:06:20,950 So all these activities put together will be demonstrated to you in the example that is falling right 68 00:06:20,950 --> 00:06:21,540 in front of you. 69 00:06:22,510 --> 00:06:31,880 And so this is the Python script, which we will use to upload any selected file to the to our WRGA 70 00:06:31,900 --> 00:06:36,880 server that we will start that the server will start running. 71 00:06:36,880 --> 00:06:41,350 As soon as you start or execute this Python script, upload IRTPA. 72 00:06:43,140 --> 00:06:44,190 So for that. 73 00:06:45,950 --> 00:06:49,130 This is a very essential import that you must. 74 00:06:50,120 --> 00:06:57,830 You know, we mentioned in our Python script at the beginning, you have to import the circular file 75 00:06:57,830 --> 00:07:00,200 name function, which is defined in the work. 76 00:07:01,690 --> 00:07:09,280 Module, which will be installed along with the flask module that you have to install to set up the 77 00:07:10,180 --> 00:07:11,170 flask in orbit. 78 00:07:12,130 --> 00:07:20,530 OK, so after doing all the required imports, you set up the flask application by declaring it, declaring 79 00:07:20,530 --> 00:07:22,030 an object of the flask class. 80 00:07:22,580 --> 00:07:31,340 This intuitively you have to do every time when you write a flask application in the rest of the protocol, 81 00:07:31,660 --> 00:07:32,410 there are two. 82 00:07:33,890 --> 00:07:36,680 You are rules that are defined, one examplar. 83 00:07:38,340 --> 00:07:41,340 Which is my favorite word, python function upload. 84 00:07:42,480 --> 00:07:45,990 And then there is a slash upload, a rule map to. 85 00:07:47,190 --> 00:07:48,240 Uploader function. 86 00:07:49,710 --> 00:07:56,820 The purpose of this absolute rule, that means the function, the function of the view, the purpose 87 00:07:56,820 --> 00:08:02,050 of this view function abroad is to render a template applauded. 88 00:08:02,970 --> 00:08:07,800 So this upload will must be present in the templates folder of your flask application. 89 00:08:09,100 --> 00:08:17,200 What is then this uploader estimate file, it is it contains a multipart formula to. 90 00:08:18,180 --> 00:08:26,130 A form with IANSITI, which is set to multi multipart and then there is a input pipe, is equal to five. 91 00:08:30,060 --> 00:08:33,600 This is our Apollodorus G.M. in which this is a form. 92 00:08:38,180 --> 00:08:48,440 This farm has one attribute called ACNC type, which is which must be said to multipart farm data whenever 93 00:08:48,440 --> 00:08:48,980 you want. 94 00:08:50,110 --> 00:08:50,770 The former. 95 00:08:52,630 --> 00:09:01,660 To have a button which will browse your computer system to select a particular file, the IANSITI tribute 96 00:09:01,660 --> 00:09:06,130 must be set to multipart oblique form data and the method also must be post. 97 00:09:07,450 --> 00:09:11,620 In order to present a browser, a browser button. 98 00:09:13,240 --> 00:09:19,210 In the form you should use, this input type is equal to file. 99 00:09:21,270 --> 00:09:28,290 The name that is appearing on that button will be fired as soon as the this button name to file is clicked, 100 00:09:28,710 --> 00:09:39,780 your Windows Explorer will be will be presented, allowing the user to select any desired file from. 101 00:09:41,340 --> 00:09:44,010 Anywhere in the computer system, filesystem. 102 00:09:45,090 --> 00:09:52,520 And then then there is a submit button, so this submit button, when it is pressed the selected file, 103 00:09:52,530 --> 00:09:55,380 the data of the selected file will be. 104 00:09:56,540 --> 00:10:03,320 Central to this at this are, which is a part of the action attributes, so action is equal to this. 105 00:10:03,320 --> 00:10:03,730 You are. 106 00:10:04,430 --> 00:10:09,680 This is, of course, the name of our server localhost 5000 uploader you are in. 107 00:10:10,640 --> 00:10:17,000 Which is defined in our Python script to this you are in this file will be said. 108 00:10:18,060 --> 00:10:18,410 Right. 109 00:10:18,840 --> 00:10:21,180 So this is the Apollodorus, David. 110 00:10:23,920 --> 00:10:26,260 Coming back to this, the uploader you are in. 111 00:10:27,810 --> 00:10:32,520 And therefore, the map review function uploader. 112 00:10:33,610 --> 00:10:39,310 What it does, it verifies whether the request method. 113 00:10:40,550 --> 00:10:48,350 That means the data that is received by these uploader, Eurail is by this post, whether or not. 114 00:10:50,050 --> 00:10:56,440 We are just seeing that in the upload estimate, the form has mythological to post, so this is true. 115 00:10:58,680 --> 00:11:05,280 So the final data is obtained by ordering from brother request, not subject. 116 00:11:06,410 --> 00:11:10,340 So every single request or files filed, so this will be the file object. 117 00:11:11,600 --> 00:11:15,800 That must be saved, if not save, this file object has a function called the SEAL. 118 00:11:18,520 --> 00:11:29,710 And this circular file name is a function in this work model which which will obtain the required destination 119 00:11:30,040 --> 00:11:32,800 filename by have not filename. 120 00:11:33,400 --> 00:11:42,510 So when this function will be successfully completed, the uploader function will return by return message 121 00:11:42,520 --> 00:11:48,970 called file uploaded successfully, which which will be displayed on the browser. 122 00:11:50,430 --> 00:11:51,420 This is, of course, the. 123 00:11:52,420 --> 00:11:55,480 Uh, this run will start. 124 00:11:56,430 --> 00:11:57,610 Our civil. 125 00:11:59,660 --> 00:12:05,870 So it's a very fairly simple, straightforward activity, this will this upload auditioner will continue 126 00:12:05,930 --> 00:12:10,210 a form with a file button and this uploader. 127 00:12:11,630 --> 00:12:14,840 You are uploader function will save the. 128 00:12:15,800 --> 00:12:16,970 File letter to. 129 00:12:18,190 --> 00:12:25,660 The file name of the same name on the server so that we are to see now we're running this application. 130 00:12:29,590 --> 00:12:33,970 So our Python script is named as uploader before we can start. 131 00:12:36,240 --> 00:12:37,740 The sermon starts running. 132 00:12:39,220 --> 00:12:41,150 And then you go back to the browser. 133 00:12:45,660 --> 00:12:50,280 And Gildas, you are in the address bar localhost, 5000 oblique. 134 00:12:51,830 --> 00:12:55,100 Upload, so as soon as hypocenter the. 135 00:12:59,630 --> 00:13:02,810 You know, button, or rather the form with the brass button. 136 00:13:04,200 --> 00:13:08,340 And a submit button will be displayed, so click on this brose. 137 00:13:10,400 --> 00:13:13,550 Select any file that you want to upload on the server. 138 00:13:14,710 --> 00:13:15,610 Uh. 139 00:13:20,390 --> 00:13:21,470 So let us say. 140 00:13:24,660 --> 00:13:26,730 I will open this file. 141 00:13:29,100 --> 00:13:30,090 And based upon. 142 00:13:31,020 --> 00:13:32,860 The selected filing will appear here. 143 00:13:33,210 --> 00:13:34,980 Now, if I press on this submit button. 144 00:13:36,030 --> 00:13:46,350 The uploader you are in will be used to send the final data and the upload function will save the file 145 00:13:46,350 --> 00:13:46,950 later to. 146 00:13:48,000 --> 00:13:52,670 If I live by thunder in the server folder. 147 00:13:56,970 --> 00:14:02,160 So it responds with the file upload very successfully, will go to our. 148 00:14:03,700 --> 00:14:09,720 Server destination and see whether the selected file has been, you know, uploaded there or not. 149 00:14:12,330 --> 00:14:20,130 So there you see deep pathology, PGD was the name of the file that was selected by using the browser 150 00:14:20,130 --> 00:14:26,100 button that is appearing in our flask application, which is the location of our. 151 00:14:26,990 --> 00:14:28,300 Development support here. 152 00:14:28,340 --> 00:14:29,240 So this is the. 153 00:14:30,470 --> 00:14:35,090 Way in which the file upload activity can be performed.