How to Restrict File Uploads to Only Accept Images

When you add a file field to a form, it can be useful to limit the selection to only images. This not only enhances user experience by avoiding wasted time and resources in uploading non-image files, but also adds a client-side filtering capability to complement your server-side filtering. To achieve this, you can utilize the accept attribute of the <input type="file"> element, specifying the MIME type of the accepted files. By setting the value to image/*, you can allow all types of images....

How to Upload a File Using Fetch

Learn how to upload files to a server using the Fetch API in a simple and straightforward way. Uploading files to a server can sometimes be a challenging task that requires hours of research. In this tutorial, I will guide you on how to upload files using the Fetch API. To begin, let’s assume you have a form with a file input field: <input type="file" id="fileUpload" /> To handle the file upload, we’ll attach a change event handler to the file input field:...

How to Upload Files in a Next.js Form

In this article, I will guide you through the process of uploading files in a Next.js form. By default, Next.js does not allow file uploads in forms, but with a few adjustments, we can make it work. First, let’s take a look at the form structure in a Next.js page: <form method="post" action="/api/new" enctype="multipart/form-data">...</form> Here, we have a standard HTML form that calls an API endpoint. Inside this form, there is a file input control:...

How to Upload Files to the Server Using JavaScript

Uploading files and processing them in the backend is a common functionality in web applications, such as uploading avatars or attachments. In this article, we will learn how to upload files to the server using JavaScript. Uploading Files Client-Side To enable file upload functionality in our web app, we start by adding an HTML file input element: <input type="file" id="fileUpload" /> Next, we register a change handler on the #fileUpload DOM element....

How to Use the FormData Object

Discover the functionality of the FormData object and learn how to effectively utilize it. The FormData object serves as a storage mechanism for preserving input field values within a form. It is particularly beneficial when you need to transmit files to a server. This object is typically only utilized in such circumstances. To demonstrate the utilization of FormData in sending file content via fetch, consider the following example: <input type="file" id="fileUpload" /> Here, an input field of type “file” is presented....

XMLHttpRequest (XHR)

The introduction of XMLHttpRequest (XHR) in browsers in the mid-2000s was a significant milestone for the Web Platform. In this article, we will explore how XHR works, its capabilities, and compare it with other popular alternatives. Introduction Back in the day, technologies like GMail and Google Maps seemed revolutionary. These applications heavily relied on XHR to deliver dynamic content. XMLHttpRequest was initially invented by Microsoft in the 1990s and became a de-facto standard as all major browsers implemented it between 2002 and 2006....