@@ -26,20 +26,27 @@ func main() {
26
26
27
27
func uploadFileHandler () http.HandlerFunc {
28
28
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
29
- // validate file size
30
- r .Body = http .MaxBytesReader (w , r .Body , maxUploadSize )
31
29
if err := r .ParseMultipartForm (maxUploadSize ); err != nil {
32
- renderError (w , "FILE_TOO_BIG" , http .StatusBadRequest )
30
+ fmt .Printf ("Could not parse multipart form: %v\n " , err )
31
+ renderError (w , "CANT_PARSE_FORM" , http .StatusInternalServerError )
33
32
return
34
33
}
35
34
36
35
// parse and validate file and post parameters
37
- file , _ , err := r .FormFile ("uploadFile " )
36
+ file , fileHeader , err := r .FormFile ("uploadfile " )
38
37
if err != nil {
39
38
renderError (w , "INVALID_FILE" , http .StatusBadRequest )
40
39
return
41
40
}
42
41
defer file .Close ()
42
+ // Get and print out file size
43
+ fileSize := fileHeader .Size
44
+ fmt .Printf ("File size (bytes): %v\n " , fileSize )
45
+ // validate file size
46
+ if fileSize > maxUploadSize {
47
+ renderError (w , "FILE_TOO_BIG" , http .StatusBadRequest )
48
+ return
49
+ }
43
50
fileBytes , err := ioutil .ReadAll (file )
44
51
if err != nil {
45
52
renderError (w , "INVALID_FILE" , http .StatusBadRequest )
0 commit comments