When the input tag is set to null, the array that holds e.target.files will be devoid of any content

Hey there, I'm currently in the process of uploading a folder. However, I encountered an issue when trying to upload the same folder again. It seems that the data was empty when I attempted to set it to null.

const uploadFolders = [];
let uploadFolderTemplate = ``;

function upload(e) {
    const files = e.target.files;

    if (files.length > 0) {
        uploadFolders.push(files);

        uploadFolderTemplate = `
            <div class="card folder-list-info">
                <div class="card-header" style="display: inline-flex;">
                    <span>${files[0].path}</span>
                    <button type="button" class="close" aria-label="Close" style="margin-left: auto">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="card-body">
                    <div class="ext-box">
                    
                    </div>

                    <div class="colum-box">
                        
                    </div>

                    <div class="exports-data-file-box">

                    </div>
                </div>
            </div>`;

    }
}


const folderUploadInput = document.querySelector('.folder-upload-input');

folderUploadInput.addEventListener('click', function (e) {
    e.target.value = null;
});

folderUploadInput.addEventListener('change', upload)

Answer №1

By saving the e.target.files object into another array and later setting the value of e.target.files to null, you may not achieve the intended outcome. This is because pushing an object into an array stores it as a reference rather than a copy. For a more in-depth explanation, refer to this answer.

To resolve this issue, you'll need to store it as a copy. There are various ways to create a copy of an object in Javascript, but one recommended method is using this approach.

const files = Object.assign({}, e.target.files);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: Unexpected character 'o' encountered in AngularJS syntax

Whenever I try to call this controller, it gives me an error. hiren.controller('hirenz' , function($scope , $http , $location , $routeParams){ $http.post((rootURL + "music") , {'alpha' : $routeParams.alpha , 'name' : $rou ...

Navigation guard error: Caught in an infinite redirect loop

I have set up a new vue3 router and configured different routes: const routes = [ { path: "/", name: "home", component: HomeView, }, { path: "/about", name: "about", component: () => ...

What benefits does using Object.create(null) offer in comparison to utilizing a Map?

I've noticed that some developers prefer using code that looks like const STUFF_MAP = Object.create(null); It seems that STUFF_MAP is intended to be used like a map, hence the choice of using Object.create(null) over {} (to avoid conflicts with prope ...

Tips for retrieving the concealed input value from the div directly preceding

Is it possible for me to retrieve the hidden input value by clicking on the text "upload profile photo"? I don't have much experience in this area. <div> <input type="hidden" value='<?php echo $list['profil ...

Easiest method to incorporate dynamic content into a statically generated page using Node.js with Express

Running a static webpage from Node.JS using the Express webserver: app.use('/', express.static('public')); What is the most efficient way to add dynamic content, like a list of items retrieved from a database, to this page? One option ...

Using javascript to locate and substitute a word divided among multiple tags - a step-by-step guide

I need to utilize JavaScript to locate and substitute a word that has been separated into multiple tags. For instance, consider the following HTML code: <html> <body> <div id="page-container"> This is an apple. ...

Adding ngrx action class to reducer registration

Looking to transition my ngrx actions from createAction to a class-based approach, but encountering an error in the declaration of the action within the associated reducer: export enum ActionTypes { LOAD_PRODUCTS_FROM_API = '[Products] Load Products ...

Node.js - Locate a specific parameter within an array that is nested within a JSON object, and retrieve the remaining

I am working with a Node.js API call that returns a JSON object. My goal is to extract the customer's phone number from this object using a specific function, like body.result.reservations[X of reserv.].client.phone, and retrieve parameters such as p ...

When using Firestore in Android, I encounter a nodejs error regarding headers being sent prematurely

Currently, I am utilizing a webview in order to display content from a nodejs server. While requesting a page works as expected, accessing firestore results in an error where it seems like nodejs is attempting to resend the page. Below is the code for an a ...

Change the label's class when the input area is selected

Looking to add a new class to a label when its corresponding input element is in focus. A form consists of 10 input fields and 10 labels, one for each field. const inputFields = document.querySelectorAll('.form-control'); console.log(inputFie ...

Error encountered on NodeJS server

Today marks my third day of delving into the world of Angular. I've come across a section that covers making ajax calls, but I've hit a roadblock where a tutorial instructed me to run server.js. I have successfully installed both nodejs and expre ...

How can I display the Bootstrap 5.3.0 Collapsible button on this basic website?

I've been struggling to implement a Bootstrap Collapsible on my web page using Bootstrap version 5.3.0. I've tried different approaches but I can't seem to get it to work. All I need is a Collapsible that contains a few links, which should b ...

Efficient communication: sending emails using AngularJS and PHP

I have developed an innovative application using AngularJS that features an email contact form which communicates with a PHP file on the server to send emails. Here is a snippet from my Controller.js file in AngularJS: $scope.feedbacksubmit= function (){ ...

The componentDidMount function is not initializing the state

I am trying to access the references from the render function and then set them to the state. Below is my code snippet: class App extends Component { constructor(props) { super(); this.arr = this.generateTimelineArray(); ...

Manipulating the actual DOM in ReactJS to display a formatted number or string while preserving its original value

Hello, I am new to ReactJS and currently using it along with ReactDataGrid. I am trying to figure out how to change the real DOM value of a specific cell in the table. Here is the table I am working with: https://i.sstatic.net/CAcSH.png My goal is to cha ...

The Truffle test encounters an issue: "Error: Trying to execute a transaction that calls a contract function, but the recipient address ___ is not a contract address."

Every time I run truffle test in the terminal, I encounter the following error message: Error: Attempting to run a transaction which calls a contract function, but the recipient address 0x3ad2c00512808bd7fafa6dce844a583621f3df87 is not a contract address. ...

Encountering a ReactJs and TypeScript error: "menuItems.map is not a function, issue with map method"

Greetings! I am currently working on implementing the logic of using useState and mapping an array to show only one dropdown item at a time. Below is my array structure with tags (menu name, links (to router), icon (menu icon), and if there are dropdown i ...

Encountering issues with tesseract callbacks in nodejs due to validation errors

I encountered the following error: fs.js:132 throw new ERR_INVALID_CALLBACK(); ^ TypeError [ERR_INVALID_CALLBACK]: Callback must be a function at makeCallback (fs.js:132:11) at Object.fs.unlink (fs.js:1002:14) at /home/bakedpanda/Documents/BTP/ ...

Navigating Users After Form Validation in React - A Step-By-Step Guide

As a newcomer to React, I am currently working on a SignUp SignIn project. My goal is to route the user to the Login/SignIn page upon successful form validation. Below is my SignUp code: import React, { Component } from 'react'; import { Link } f ...

Error encountered: A missing semicolon was detected before a statement while executing code within a for

Although this question may have already been asked, I am struggling to understand why it is not working as expected. I simply want to increment the markers array within a for loop and then add each marker to the vector source using vectorSource.addFeature ...