Switch from file input conversion button to uploaded file button with Angular rendering the file name

I am looking to enhance the file upload feature in Angular 6 by replacing the browse button with an upload button once a file is selected. Can anyone provide guidance on how to achieve this?

Currently, I have created the initial screen as shown below:

HTML

  <div class="pr-3 pt-2">
    <h4>Choose an excel file to upload</h4>
  </div>
  <div>
    <label for="upload" class="file-upload-label">Browse</label>
    <input type="file" placeholder="Upload file" accept=".xlsx" (change)="incomingfile($event)" class="file-upload-input" id="upload">
  </div>

Incoming file event function

 incomingfile(event) {
    this.file = event.target.files[0];
    this.filePresent = true;
  }

https://i.sstatic.net/muBCB.png

Answer №1

Introduction to Basic Logic -----> Check out the DEMO

Here is the HTML code snippet:

<div class="pr-3 pt-2">
    <h4>Select an excel file to upload</h4>
</div>
<label *ngIf="!filePresent">
            <input #fileInput  type="file" (change)="incomingfile($event)" />
            <span>Browse File</span>
</label>
<label *ngIf="filePresent">
            <label >{{fileName}}</label>
<button (click)="uploadFile()"><span> Upload</span></button>
</label>

And here is the corresponding TypeScript code:

  file: File;
  filePresent: boolean = false;
  fileName: string = ''

  incomingfile(event) {
    this.file = event.target.files[0];

    this.fileName = this.file.name;

    this.filePresent = true;
  }

  uploadFile() {
    if (this.file) {
      console.log(this.file);
    }
  }

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

Customizing the Navigation Border-Bottom in WordPress

Currently, I am working on a project with this website. My main focus is on removing the border-bottom in the navigation bar for the last two right navigation points which are "DE" and "FR". Despite trying various CSS solutions, such as: #menu-item-394 { ...

What is the best way to set up <Input> to restrict input to only include letters, spaces, and the backspace key

I have created a 15 by 15 matrix displayed as an HTML table where each cell contains an input box. I am looking to restrict user interaction so that they can only: Input English letters Delete characters using backspace Use the spacebar In the future, nav ...

When I have a lengthy description, the alignment of the CSS DIV prices becomes misaligned

When I have a long description, the prices appear misaligned. You can take a look at it here: https://i.sstatic.net/aXgaj.jpg For the JSFiddle example, visit: https://jsfiddle.net/2n19tv75/ .border-gray { border: 1px solid #e5e7eb; float: left; w ...

What is the best way to manage multiple write functions triggered by a socket.io room's `emit` event?

Let's start with some context before diving into the question below. Context I am currently developing a browser-based JavaScript game where players join a virtual room and interact by moving tiles. Once the game concludes, user data is sent to the ...

Updating form validation by altering input value

I'm currently utilizing JavaScript regular expressions for form validation in my project. After successfully completing the validation without any errors, upon clicking the submit button, I want the form to be submitted and change the submit value to ...

The loader function for createBrowserRouter in React Router is causing an infinite loop

I have encountered an issue with implementing loader functions on my browser router. The functions trigger an API call that updates a context state value. However, updating the state is causing the loader function to run again, resulting in an infinite loo ...

Unable to access account with Passport.js after cookie expiration

Currently, I am utilizing Passport JS as my authentication middleware with a local strategy. The authentication process works smoothly until the cookie expires, and then I am unable to login. passport.use(new LocalStrategy({ usernameField: 'email ...

Controlling File Upload Edits: A Guide to Effective Management

I am facing an issue on my product page related to adding and updating products in the database. The problem lies with images not displaying correctly. During the product insertion process, everything works fine. In my aspx page, I have the following code ...

Need help triggering an alert once your AJAX post is successful? Here's how!

Within my Node application, I have implemented both get and post requests. The get request is responsible for rendering the page, while the post request inserts data into a MySQL database. Although the data is inserted correctly, the alert message fades aw ...

How can one change the color of movable tiles to red while keeping the rest of the tiles in their original state in a puzzle game?

In this section of our code, the function moveTile is responsible for moving a tile. How can I modify it so that the hover color changes to red? function moveTile(identity) { var emptyId = findEmptySpace(); if (isEmptyNeighbor(identity)) { document.ge ...

unable to properly evaluate the string results from the AJAX request

Utilizing AJAX to verify a user's login information for accuracy, I am encountering an issue where the comparison between the returned "success" String and the expected value using == or === always results in failure. The AJAX implementation is as fo ...

How can we use jquery ajax to insert data into a JSON array?

I am struggling to identify the mistake in my AJAX code. Despite following tutorials online, I am unable to successfully POST data. Here is my HTML code: <h3>Input new data</h3> <form name="contact"> <input type="text" placeholder ...

Unable to pass data from a Jquery ajax request to another function

I've written a basic ajax request using jQuery. Here is the code for my ajax function: var sendJqueryAjaxRequest = function(arrParams) { var request = $.ajax({ url: arrParams['url'], async: false, ...

Can halting an ajax function mid-process prevent it from finishing?

My objective is to convert a video using ffmpeg, which tends to take a considerable amount of time to complete. I'm considering sending an ajax request to the server for this task, but I don't want the user to have to wait until the video convers ...

What is the best way to connect CodeIgniter with Angular.js?

Are you facing challenges while integrating Angular.js and CodeIgniter? In my main.js, I'm utilizing ngRoute to define routes as follows: $locationProvider.html5Mode(true); $routeProvider.when('/test', { templateUrl: 'partials/te ...

Implement the addition of subcollections to a unified identification document in Firestore

I am struggling to store the URLs of multiple images (previously saved in Storage) in a subcollection of a Firestore document. Although I have managed to do it, each image generates a new document with its corresponding sub collection img, which is not my ...

How to pass parameters from ajax to php with the help of slim framework

I keep encountering a 404 error when attempting to pass a variable or constant to PHP using slim. http://localhost:5000/project/1 (Not Found) AJAX $.ajax({ url: path + '/project/1', type: 'get', dataType: ...

Providing status responses following the usage of Promises for onload and onerror situations

I am currently experimenting with using JavaScript's onload and onerror functions to determine if a page or image loads successfully. However, I am facing difficulties in accessing the status variable when trying to assign it to read the status. Addit ...

Receiving input from the "Enter" key is functional in Internet Explorer but not Firefox when utilized in Cognos

Working with Cognos, I'm currently tackling the challenge of capturing the enter key to validate user input before submitting the prompt. If you're not familiar with Cognos, that's okay - any insights on my code are welcome :) This tool is ...