Having trouble retrieving filenames using JavaScript with FileUpload?

I am having trouble retrieving filenames and displaying them in the label lbName. I tried using asp:FileUpload but it's not working. Here is my code:

<asp:FileUpload ID="FileUpload1" AllowMultiple="true" runat="server" Onchange="upload()"/>                      <asp:Label ID="lbName" runat="server" ForeColor="Gray" Visible="True"></asp:Label>

Here is the JavaScript code I am using:

function upload() {
    var name = "";

    var files = document.getElementById("<%= FileUpload1.ClientID %>");
    for (var i = 0; i < files.length; i++) {
        name = name + (files[i].name) + ";";
    }
    document.getElementById("lbName").value = "1: " + name;
}

Answer №1

To enable file uploads, you can utilize jQuery by attaching a change event listener to the input type=file.

<script type="text/javascript">

    $('input[type="file"]').change(function (e) {
        var files = [];
        for (var i = 0; i < $(this)[0].files.length; i++) {
            files.push($(this)[0].files[i].name);
        }
        $(this).next('span').html(files.join(', '));
    });

</script>

If you prefer pure javascript functionality:

<script type="text/javascript">

    function upload() {
        var name = "";
        var files = document.getElementById("<%= FileUpload1.ClientID %>");
        for (var i = 0; i < files.files.length; i++) {
            name = name + (files.files[i].name) + ";";
        }
        document.getElementById("<%= lbName.ClientID %>").innerHTML = "1: " + name;
    }

</script>

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

Learn how to implement autofocus for an ng-select element within a bootstrap modal

When working with ng-select inside a modal, I am facing an issue with setting autofocus. While I am able to add focus for the input field within the modal, the same approach doesn't work for ng-select. Can anyone provide guidance on how to set focus f ...

Using AngularJS to send a 2D array via POST request

How to use Angular HTTP POST $http({ contentType: "application/json; charset=UTF-8", url: "myCSharpMethod", method: "POST", data: myData, // 2D array ( myData[[],[]] ) traditional: true }) .success ...

Is it truly possible to return a reactive variable that updates its value asynchronously?

While reviewing the code of a frontend project developed in Vue3, I came across a unique construction that I have not encountered before. This has led to some confusion as I try to grasp how it operates. The concept involves assigning the result of an asyn ...

Define a property within an object literal that sets a function's property

I am defining a new tasks object: var tasks = { test: () => { /// .... } }; Within the test function, I am attempting to assign a value to the tasks.test.description property. Despite my efforts, such as: var tasks = { test: () ...

The connection to port 7054 for RemoteWebDriver could not be established within the allocated 45000 milliseconds

I am utilizing the .NET binding for WebDriver to execute tests concurrently through Grid2. Running tests individually poses no issue, however, when running multiple tests via the Grid, an occasional error occurs. Once this error surfaces, it often leads to ...

sending data directly from the ViewModel to the controller instead of passing it through another class (files) in the view

FunwarsVM.cs public class FunwarsVM : IFunwars { public int Id { get; set; } public string Date { get; set; } public string OurTeam { get; set; } public string Status { get; set; } public string Opponent { get; set; } public string ...

Performing tasks when a component is fully loaded in Vue.js Router

I am currently working on a project involving a single-page application built with Vue.js and its official router. For this project, I have set up a menu and a separate component (.vue file) for each section that is loaded using the router. Inside every c ...

Is there a way to customize setInterval for a specific element?

For some time now, I've been working on creating a typing animation that triggers when links come into view during scrolling. However, I've encountered a problem with jQuery not selecting the correct element or the setInterval objects causing all ...

Incorporate React to import a file from the parent directory

How can I import a js file called 'usercontent.js' into a react component when the current js file is located in the Component Folder and 'usercontent.js' is located within the Conponent Container folder, which itself is a subfolder of ...

Using JavaScript in PHP files to create a box shadow effect while scrolling may not produce the desired result

Issue at hand : My JavaScript is not functioning properly in my .php files CSS not applying while scrolling *CSS Files are named "var.css" #kepala { padding: 10px; top: 0px; left: 0px; right: 0px; position: fixed; background - c ...

What is the best way to retrieve a list of customers within a specified date range?

How can I retrieve a list of customers within a specified date range? My frontend react app includes starting and ending date pickers, but I'm unsure how to query the data using mongoose in my express app. Below you'll find my mongoose model and ...

How to achieve an endless cycle using Promise recursion in a NodeJS environment

I am planning to replace my blocking infinite while loop with promises. My run function is quite simple - it lights up an LED and then turns it off before moving on to the next one. Since Promises do not work inside while loops, I'm exploring how I c ...

Switching Bootstrap Navbar Active State with JavaScript

I have encountered an issue with using the "active" class on my navbar navigation items in Bootstrap 4. When I click on the links, the active state does not switch as intended. I have tried incorporating JavaScript solutions from similar questions but have ...

Display a preview window once the image has been chosen

I have created an image preview div to show the selected image's thumbnail. Everything was working fine so far. But now, I want this div to be hidden when the page initially loads and only become visible when a user selects an image to upload. Here is ...

Change HTML table information into an array with the help of JavaScript

I am facing an issue where the array is displaying as undefined even though I am storing table data in an array. Check out more about arrays here. Attempting to retrieve each problem's row in the form of an array function getAllProblemRowElements() ...

Optimizing ASP.NET: Effective method for preserving label.text data during postback utilizing PageMethods

AngularJS, $http service. Hello, I am currently utilizing AngularJS with the $http service to dynamically update the content of a text box when a user selects an option from a dropdown menu on my web page. It has come to my attention that the text in te ...

Utilizing ASP.NET with ModalPopupExtender for a Smooth Click Interaction

Operating an ASP.NET software utilizing ASP.NET AJAX, employing the ASP.NET AJAX Toolkit to exhibit a dialog for users. When users click 'Yes' on the dialog, intending to manage that event in the code behind but realizing that the click event isn ...

Having trouble retrieving the tag name, it seems to be giving me some difficulty

I have two separate web pages, one called mouth.html and the other nose.html. I want to retrieve a name from mouth.html and display it on nose.html when a user visits that page. How can I accomplish this using JavaScript? Here is the code snippet from mou ...

Overcoming Troublesome Login Input Box in Web

In an effort to streamline tasks in our office, I am working on automating certain processes. Specifically, this program is designed to log into our insurance company's website and extract information for payroll purposes. Below is the code snippet th ...

Showing the value of a variable in the select dropdown field while editing using React Bootstrap

When it comes to editing, I am using input select. The ant design framework successfully displays the content of the status variable in the input field. However, in the react-bootstrap framework, I am facing issues with displaying the content of the status ...