Intermediate LevelHTML & Semantic Web
Why is enctype="multipart/form-data" required for HTML file upload forms?
❓ Interview Prompt: Explain enctype attribute, application/x-www-form-urlencoded vs multipart/form-data, and input type="file".
By default, HTML forms submit data using 'enctype="application/x-www-form-urlencoded"', which converts form data into a single text string.
Why 'multipart/form-data' is Mandatory for Files:
Binary files (images, PDFs) cannot be url-encoded into plain text strings without corruption. 'enctype="multipart/form-data"' breaks the request body into distinct boundary-separated MIME parts, allowing binary files and text inputs to be transmitted safely!
Code Implementation Example
html
<form action="/api/upload" method="POST" enctype="multipart/form-data">
<label for="avatar">Select Avatar Image:</label>
<input type="file" id="avatar" name="avatar" accept="image/png, image/jpeg">
<button type="submit">Upload File</button>
</form>💡HTML file upload form using multipart/form-data encoding and accept attribute.
🎯 Interview Key Takeaway
Set enctype="multipart/form-data" on POST forms containing input type="file" to transmit binary files.
#enctype#multipart/form-data#File Upload#Forms#Intermediate
Back to all HTML & Semantic Web Questions→