Angular FormData fails to append and upload files

I am attempting to use FormData in order to upload a file through an HTTP Request. Here is the HTML code:

<ng-template #displayApp>
    <div class="display flex justify-content-center">
        <div >
            <p-fileUpload 
            chooseLabel="Select First File"
            [showUploadButton]="false"
            [showCancelButton]="false"        
            (onSelect)="checkFilesSelected()"
            (onRemove)="checkFilesSelected()"
            #originalFile>
            </p-fileUpload>
        </div>

        <div style="margin: 0 20px;"></div>

        <div >
            <p-fileUpload 
            chooseLabel="Select Second File"
            [showUploadButton]="false"
            [showCancelButton]="false"             
            (onSelect)="checkFilesSelected()"
            (onRemove)="checkFilesSelected()"
            #revisedFile>
            </p-fileUpload>
        </div>
    </div>
    <div style="margin-bottom: 20px;"> </div>
    <div class="display flex justify-content-center">
        <p-button [disabled]="!areFilesSelected" (click)="onUpload()">Compare Files</p-button>
    </div>
</ng-template>

I am choosing two files and attempting to upload both files together with a single call to keep them synchronized.

Here is my component's TS file:

  @ViewChild('firstFile') firstFile!: FileUpload;
  @ViewChild('secondFile') secondFile!: FileUpload;

onUpload() {
    console.log("File upload called",);

    const originalFiles: File = this.originalFile.files[0]
    const revisedFiles: File[] = this.revisedFile.files;
    let formData: FormData = new FormData();

    console.log("First ",originalFiles, originalFiles.name)
    debugger;

    formData.append('First', originalFiles)
    console.log("Form Data ", formData)

    let uploadUrl = new URL('baseURL');

    uploadUrl.searchParams.append('First',"first");
    uploadUrl.searchParams.append('Second',"second");  

    this.http.post(uploadUrl.toString(), formData).subscribe(
      response => {
        console.log('File uploaded successfully:', response);
      },
      error => {
        console.error('Error uploading file:', error);
      }
    );
  }

I have noticed that originalFiles gets populated with file details. However, when I try to append it to formData, it remains empty without throwing any exceptions. The formData appears empty when viewed on the console. Any assistance on this issue would be greatly appreciated.

I attempted to change the data type to File instead of FileUpload, but it did not resolve the issue. Using event.Files[0] appends the file correctly. I am unsure how to merge this event into a single one for file uploads.

Answer №1

When working with form data in Angular, it is crucial to ensure that the enctype header is properly set. Checking the values within the FormData can be done using the

entries method.</p>
<pre><code>onUpload() {
  console.log("Initiating file upload");

  const originalFiles: File = this.originalFile.files[0];
  const revisedFiles: File[] = this.revisedFile.files;
  
  let formData: FormData = new FormData();
  formData.append('First', originalFiles);

  // Logging each entry in the formData
  for (const [key, value] of formData.entries()) {
    console.log(`${key}: ${value}`);
  }

  let uploadUrl = new URL('baseURL');
  uploadUrl.searchParams.append('First', "first");
  uploadUrl.searchParams.append('Second', "second");

  const headers = new HttpHeaders({ 'enctype': 'multipart/form-data' });

  this.http.post(uploadUrl.toString(), formData, { headers: headers }).subscribe(
    response => {
      console.log('File uploaded successfully:', response);
    },
    error => {
      console.error('Error uploading file:', error);
    }
  );
}

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

I rely on the angular-responsive-carousel library for my project, but unfortunately, I am unable to customize the arrow and dots

When it comes to CSS, I utilize ng deep style in Angular 10 to make changes for browser CSS. However, I am facing an issue where the problem is not being resolved by my CSS code. Here is a snippet of my code: > ::ngdeep .carousel-arrow { > b ...

Utilize the powerful Syncfusion Essential JS 2 Grid for Angular 5 to seamlessly export your data to Microsoft

Looking at the documentation for syncfusion-ej2 Grid, I noticed that it includes features such as 'PDF export' and 'Excel export'. I have successfully implemented these features in my Angular application. However, I have been unable to ...

I'm having trouble asynchronously adding a row to a table using the @angular/material:table schematic

Having trouble asynchronously adding rows using the @angular/material:table schematic. Despite calling this.table.renderRows(), the new rows are not displayed correctly. The "works" part is added to the table, reflecting in the paginator, but the asynchron ...

What is the best way to transform a Storybook typescript meta declaration into MDX format?

My typescript story file is working fine for a component, but new business requirements call for additional README style documentation. To meet this need, I am trying to convert the .ts story into an .mdx story. However, I am facing challenges in adding de ...

Having trouble importing or resolving files with ts-loader or css-loader?

Struggling to incorporate css modules by utilizing style-loader and css-loader in my project. I am facing difficulties understanding the root cause, unsure if it's ts-loader or css-loader to blame. webpack.config.js const path = require('path&a ...

Having troubles with *ngFor in Angular 8? Learn how to use ng-template effectively

I need assistance creating a table with dynamically generated columns and using the PrimeNg library for the grid. Despite asking several questions, I have not received any responses. Can someone please help me achieve this? To generate table column heade ...

The error message "printer.node is not a valid Win32 application" indicates that the

I created a node API for my Angular application that utilizes the node-printer package to print PDF files generated by node. However, when I attempted to run my application using nodemon, an error occurred. Error message: "node printer.node is not a val ...

Troubleshooting a problem with Angular2 involving the This.http.get function

Currently, I am delving into Angular 2 and have set up an ASP.NET WebApi locally hosted in IIS at http://localhost:8081/ping. The API call successfully returns a string serialized as a JSON Object. Below is the code for my service: import { Injectable } ...

Creating an Angular 2 component that utilizes an interface within the constructor

If you have an interface named IData, and you want to develop an Angular 2 component that can accept any Class utilizing IData in its constructor, could this concept be implemented or is it off track? Your insights are greatly appreciated. ...

Angular 2 - Directive fails to work if not properly imported into its module

Trying to use a directive across multiple modules in Angular can be tricky. If you declare it in a shared module and import that module into other modules, you might encounter errors. It seems like the directive only works when declared directly within the ...

Can you explain the distinction between `any[]` and `{ [s: string]: any }`?

I was attempting to make this code snippet function properly: test(a: any[]) { let b: string[][] = []; b.push(Object.keys(a[0])); b.push(...a.map(e => Object.values(e))); } However, the compiler is throwing an error for the b.push(...a.ma ...

How to dynamically load a component within a class-based Vue component

I am facing an issue with loading two components dynamically using an object map. Info (options-based) SearchBar (class-based) While it works for the options-based component, I encounter an error stating _currentTab is undefined when trying to load a si ...

Retrieve the injectable value when importing SubModule into the App Module

Let me provide some background information... I have a feature module that requires a string value to be passed to its forRoot static method when imported in app.module.ts, like this: @NgModule({ declarations: [ /* ... */ ], imports: [ My ...

Tips for updating the value within a textfield in HTML

I am looking to dynamically update the value displayed in my Revenue textfield by subtracting the Cost of Goods from the Sales Price. I have included an image of the current layout for reference, but I want the Revenue field to reflect the updated value af ...

One of the essential components in Angular is currently absent

After integrating Angular4 with Visual Studio 2017 following the steps outlined in this article, I realized that my setup also involved using Nodejs 8.6.0 and npm 5.4.2 - which were the latest versions at the time. Despite having vs2017 generate a fold ...

Typescript validation of tokens using Azure functions

Currently working on a website utilizing Azure Static Web App, where the login/registration is managed by Azure B2C. The backend API consists of typescript Azure functions integrated with Azure Static web app. Certain API calls can only be accessed when th ...

"Organizing Data with Angular 2's Sorting Pipe for Arrays of

Guide to Creating a Sorting Pipe in Angular 2 Using an Array of Objects Initial Challenge: I am working with a TODOs list (Todo[ ]), and I need to sort it every time modifications are made. The desired outcome is to have completed todos appear at the bot ...

Strategies for obtaining the return type of a map and only including the type S

How can I retrieve the return type of map and only display S? const initialState = { x: 1, y: 2 } type InitialStateType = typeof initialState type MapGetter<S, R, R1> = { map: (state: S) => R mapB?: (state: S) => R1 // more ... } ...

Can we limit the return type of arrow function parameters in TypeScript?

Within my typescript code, there is a function that takes in two parameters: a configuration object and a function: function executeMaybe<Input, Output> ( config: { percent: number }, fn: (i: Input) => Output ): (i: Input) => Output | &apos ...

Inserting a pause between a trio of separate phrases

I am dealing with three string variables that are stacked on top of each other without any spacing. Is there a way to add something similar to a tag in the ts file instead of the template? Alternatively, can I input multiple values into my angular compo ...