How to Add an Image Uploader using JavaScript and HTML

Now that we have come to depend so heavily on the internet and social media when it comes to our jobs, it’s important that we understand how to use certain platforms to make our lives that much easier. When you are a web developer and working on a project, you will need to upload images to your website or server. The ability to upload pictures and other files into a project is a crucial requirement for web applications. Both HTML and JavaScript offer support for file upload. In this article, we will discuss the steps you can take to add an image uploader using JavaScript and HTML.

Update: You can now simplify your uploading by using file upload widget!

JavaScript Drag and Drop Image Uploader

In the JavaScript file, the first thing is to get access to the input field where the drag and drop option is used to upload images. When using the drag and drop functionality to upload a file into the browser from the operating system, the browser may try to open it by default. This kind of default behavior should be prevented to ensure the event will not crush the uploading system.

Drag events usually take different forms. When developing the drag and drop file dropping, the following events are required.

  • Dragcenter: a dragged file enters the intended drop target
  • Dragleave: the dragged file should leave the drop target
  • Dragover: the dragged file is moved over a valid drop target
  • Drop: the file is finally dropped in over the intended drop target.

While performing any of these steps, the uploader should indicate that the file is moving around the target drop region. You can select a file from your local hard drive storage, but there are certain measures you should take.

Validating Images

When you use the drop and drag function, it should validate two aspects that include the file type and file size. The common file types that you can upload using this functionality are JPG, GIF files, and PNG. However, you also have an option of adding or removing other forms of files depending on your needs.

Validating file size is another step you should take to prevent the disruption of the file uploading process. Files that are too big may fail to upload, so you should choose a reasonable maximum size of files to upload. For instance, you can limit all the images to 10 MB to ensure a smooth uploading process.

Client validation is advantageous in that it helps us to know instantly whether the file we are trying to upload is valid. Total reliance on server validation can take quite a bit of time since the user should wait until the upload process is complete and the server processes the response. If the image files are too big, this can pose a challenge to the server. However, you can check if the file is validated on both sides to see if it has been uploaded successfully.


Preview images

There are two preview JavaScript methods you can consider when uploading a file to a browser. You can preview the file before uploading it, and this method is fast and efficient. The other option is to preview the image after uploading. You should obtain the image URL and check the appearance of the file. File preview helps you determine if the upload has been successful. If you are not satisfied, you can make the necessary changes to ensure that your project is perfect. Whatever method you choose, you should make sure you upload quality images to your website.

HTML File Upload

When you choose the HTML file upload option, the first thing you should do is to create a place for the input field and image. The input field consists of an accept attribute, and you can use it to show the acceptable file types you can upload. PNG and JPG are good examples of files types you may want the program to accept. The next step is to define the exact location where you will upload the pictures.

This is also called a drop zone, where you will drag and drop all the images to upload. It can be accompanied by a section that shows the uploaded content. The content area and the drop zone will be identified by their respective IDs. Additionally, the div will also be used with an ID showing the display of the image and the exact location where it will go. By giving an image a div, you will be able to create an appropriate height and width. This will lead to the creation of a suitable image that perfectly fits the parent container.

The next step is to define what you want to do exactly after the image has been dropped. A drop event listener will be required for that particular task. Upon successful upload of an image, File API will make it easy to read the File object via a simple JavaScript code. You must listen to the change event of the image uploader to be able to read the File object. You must obtain the file uploader instance by the ID and include the change event listener if you want to read the file object. This can be possible when the upload process completes.

Use the Upload Functionality

Upload.js offers end-to-end file upload functionality you can use for uploading images to your website. The first thing you should do is to install Upload.js through a script tag or a special package manager provided. You also need to code from the link provided to create a file upload button which you will use for uploading images on your web page. Once you create the upload functionality, you can use it for various purposes, and there are certain steps you should follow. The following are some of the activities you can do using this function in your project.

Simple File Upload

You can specify the exact input type if you want to use the file upload functionality in your web application. An input file will allow you to upload one or more files by using the button provided. You can also upload a single file using the OS’s traditional file browser. However, this is a default setting. Upon successful image upload, the File API will make it possible for the user to read the File object through the use of a simple JavaScript code. You must listen to the change event in the file uploader to be able to read the File object.

You must access the file uploader instance by ID then convert the event listener to read the file object after the completion of the upload. You should monitor the output produced in the browser console to access all the metadata details about the file you have uploaded. This information will help you understand the status of the file.

Multiple File Uploads

You can use the HTML upload functionality to upload several files at the same time. You can achieve that by adding an attribute called multiple to your file input tag. When you do this, the browser can allow you to upload multiple files. You can also add the change event handler if you want to capture the details about the files being uploaded. The FileList will appear as an array and guide you throughout the process.

When you upload multiple files to a browser or your website, the image uploader will show the progress of the upload. However, you should know that the upload process can be disrupted as a result of network issues or the size of a particular folder. The uploader will provide all the information, and you can also cancel the process if you want to change the files. For multiple files, the uploader will divide them into smaller, manageable chunks. It can also retry to load the files at a later stage if the process cannot be completed.


File Metadata

The other important thing you should know is that whenever you upload a file, the File object provides metadata information like file size, name, type, and last update time. This information plays a critical role in making further validations during file uploads.  With the ideal information, you can make an informed decision. The details will also tell you what you should do if you want to make some changes to the uploaded files. You can always revisit the files and make the necessary adjustments.


File Accept Property

The file accepts property is another crucial component you should know when you choose to upload your documents using HTML. You can use this attribute to control and determine the types of files you can upload to your browser. The file browser can accept specific forms of images like PNG, JPG, or PDF.

When you have this information, you can make the entire uploading process easier since the browser will automatically select the files. The manual process of selecting images for upload can be time-consuming, and this is where the file accept option comes in handy. You can change it to all files if you want, but it is a good idea to deal with a specific type of file.


Manage File Content

When you upload images to your browser using HTML, you might need to display the file content once it has been successfully uploaded. This feature allows you to sort your pictures, and you can allocate them to different categories. It may be confusing to identify the picture you want to use on your profiles if they are not labeled. You can utilize the FileReader object to change the images to a binary string. You can add the load event listener to obtain the binary string when the files upload successfully. Information about different files also helps you organize your website, so the visitors can quickly locate the details they want.


Validate File Size 

File validation is another essential element you should prioritize when you upload images using HTML. This option allows you to read the size of the file before you upload it to a server. You can use it to set a limit on the size of the files that can be uploaded at a particular time. The uploading process can be interrupted if you force to upload huge files. This can also be an exercise in futility when you try to upload a file that will be rejected at the end of the day. Therefore, the user will know the ideal types of images they can handle before wasting their time.


File Upload Progress

For improved usability of the file upload system, the user should know about the progress. This option helps you know the size of the file that has been loaded and the time remaining to complete the process. The HTML5 progress tag can be used to create a progress bar with all the details you may need about the uploading process. When the file is too big, the progress bar will help you make an informed decision. For example, you can resize the image and try to load it again.


Directory Upload

Uploading the entire directory is possible, but it comes with some limitations. There is a special attribute called webkitdirectory you can use to upload the entire directory if you want. It was initially designed for WebKit-based browsers, but it is now compatible with Firefox 50 and Microsoft Edge. This is still not standard, so you should avoid using it unless you don’t have a better alternative. The FileList will contain all the information about the documents that have been uploaded to the directory. For each object you upload, it will have a traceable directory path.


When you are working on a web-based project, you must have the correct tools for uploading images. For instance, native HTML features and JavaScript are viable options that can help you accomplish your goals. File upload offers several smart options by default. The drag and drop is another file upload method you can consider. While the drag and drop functionality might require some effort to create, it is a viable option. Alternatively, you can select a library with the option.

About the author

With a passion for Knowledge, Smashinghub has been created to explore things like Free Resources For Designers, Photographers, Web Developers and Inspiration.

Twitter Visit author website

Subscribe to Smashing Hub

Comments are closed.