0

I am curious to know how image is uploaded in an HTML form. I have seen at lots of places, that an image is being attached(like Gmail) or lots of other places where we select an image from disk to be processed on server.

So when exactly is the image is uploaded?

  • Just with the moment I choose an image.
  • It is encoded base64 and then sent with the form data.

Which one of these is true? And which should be used when?

SomeRandomDev
  • 129
  • 11
Kuldeep Yadav
  • 1,664
  • 5
  • 23
  • 41

1 Answers1

1
  1. When you click on choose file , file is attached .
  2. When you click on form submit button , the form is submitted with selected file.

the image file is not encoded as base64 , it is sent to server as type of multipart

you need to set enctype="multipart/form-data" in your html form when you allow file to upload

<form action="" method="POST" enctype="multipart/form-data">
 <input type="file" name="file">
 <button type="submit">Submit</button>
</form>

you can set acceptable file formats in input type file

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71