Home > Web Development > Upload multiple images with preview and easy delete.

Upload multiple images with preview and easy delete.

Diego Cortés
Diego Cortés
January 19, 2025
Upload multiple images with preview and easy delete.

In the digital age, sharing images has become an essential part of online communication. Whether for social media, websites, or applications, the ability to upload multiple images quickly and easily is a necessity. This article explores how to implement a feature that allows for the uploading of multiple images with preview and the option for easy deletion in a web environment.

Why is multi-image upload important?

The functionality of uploading multiple images enhances the user experience, facilitating the management of visual content. Allowing users to select several photos at once not only saves time but also makes the process more efficient. This is especially relevant for e-commerce platforms, blogs, or social media, where images are crucial for attracting and retaining visitors.

Implementing image upload

Using jQuery

To implement the upload of multiple images with preview, jQuery is an effective tool. This popular JavaScript framework simplifies DOM manipulation and event handling.

A basic code to allow the selection of multiple image files can be achieved using an HTML input of type file. Here’s an example:

<input type="file" id="imagenes" accept="image/*" multiple>

This code allows the user to select multiple images at once. The accept property is used to restrict selectable files to images only.

Previewing selected images

Once images have been selected, the next step is to display a preview. This can be done using the JavaScript FileReader object. The following code snippet shows how to do this:

document.getElementById('imagenes').addEventListener('change', function(event) {
    const archivos = event.target.files;
    const contenedor = document.getElementById('vista-previa');
    contenedor.innerHTML = ""; // Clear any previous previews

    for (let i = 0; i < archivos.length; i++) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const img = document.createElement('img');
            img.src = e.target.result;
            img.height = 100; // Adjust the height of the image
            contenedor.appendChild(img);
        }
        reader.readAsDataURL(archivos[i]);
    }
});

In this code, each time the user selects images, a preview is generated from the selected files. The images are displayed in a specified container in the HTML.

Easily deleting images

In addition to uploading and previewing images, it’s important to provide an option to delete unwanted images before final upload. This can be achieved by adding a delete button for each image.

A simple approach is to create a button that removes the image from the preview. Here’s a straightforward version of the code that includes this functionality:

function eliminarImagen(imgElement) {
    imgElement.remove(); // Remove the image from the container
}

By adding this method to each of the displayed images, the user can access a "delete" button that, when clicked, hides and deletes the image from the preview.

Conclusion

Easily uploading multiple images, with preview and delete options, is a feature that significantly enhances the user experience on any digital platform. Implementing this feature is relatively simple, thanks to tools like jQuery and the use of JavaScript.

I invite you to explore more news and information about web development and technology on my blog. Discover how to enhance your technical skills and stay updated in the digital world!

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 23.94 ms