Having trouble with Angular Ng2-file-Upload's Upload.all() method not successfully sending files to the API

Dealing with the challenge of uploading files in mp4 and jpg formats, I have set up 2 separate instances of FileUploader with custom validation. Upon clicking the upload button, I attempt to merge the files from both instances into a single FileUploader and trigger the upload all method to send the files to the server (API Service). However, I am encountering an issue where the server is not being hit. Below is the code snippet outlining my attempts to resolve this problem - any assistance would be greatly appreciated.

File Uploader Initialization

    uploader: FileUploader;
    coverImageUploader: FileUploader;
    mergedFileUploader: FileUploader;
    options: FileUploaderOptions = {
        url: URL,
        authToken: `Bearer Token`,
        authTokenHeader: 'authorization',
        isHTML5: true,
        method: 'POST',
        itemAlias: 'file',
        headers: [{
            name: 'refId',
            value: ''
        }, {
            name: 'userId',
            value: ''
        }, {
            name: 'roleId',
            value: ''
        }]
    }

        this.uploader = new FileUploader(this.options);
        this.coverImageUploader = new FileUploader(this.options);
        this.mergedFileUploader = new FileUploader(this.options);
 

Combining Files from Multiple FileUploaders

      let files: any = []  
      files.push(this.uploader.getNotUploadedItems().filter((f: FileItem) => !f.isUploading))  
      files.push(this.coverImageUploader.getNotUploadedItems().filter((f: FileItem) => 
      !f.isUploading)) 

var merged = [].concat.apply([], files);
        merged.forEach(e => {
            e.options.headers.find((o, i) => {
                            if (o.name === 'refId') {
                    e.options.headers[i] = {
                        name: 'refId',
                        value: e.formData.reduce(r => r).RefId

                    };
                    return true;
                } else if (o.name === 'userId') {
                    e.options.headers[i] = {
                        name: 'userId',
                        value: user.userId.toString()
                    };
                    return true; 
                } else if (o.name === 'roleId') {
                    e.options.headers[i] = {
                        name: 'userId',
                        value: user.roleId.toString()
                    };
                    return true; 
                }
            })
        })
        this.uploader.clearQueue();
        this.coverImageUploader.clearQueue();
        this.mergedFileUploader.clearQueue()
        this.isUploadProcessing = true
        this.mergedFileUploader.addToQueue(files)

Handling Upload Button Click Event

UploadFiles(){
this.mergedFileUploader.uploadAll()
}

Answer №1

Struggling with the file-uploader component, but you could experiment with a basic FileReader in JavaScript to simulate the same functionality (reading file contents and then sending a necessary server request using an HTTP client service). Check out this resource for more details:

https://javascript.info/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

What is the definition of a non-arrow React functional component in TypeScript?

Defining types for a React functional component in TypeScript can be done like this: export const Component: React.FC = () => { return // Content }; But how would you define the types for a non-arrow function? function Component() { return // Con ...

Guide to Generating a Compilation Error with Method Decorators in Typescript

Currently, I am developing a library named expresskit which allows the use of decorators to define routes, params, and other functionalities for express. While refactoring the codebase, I am considering implementing restrictions on the types of responses a ...

Overlooking errors in RxJs observables when using Node JS SSE and sharing a subscription

There is a service endpoint for SSE that shares a subscription if the consumer with the same key is already subscribed. If there is an active subscription, the data is polled from another client. The issue arises when the outer subscription fails to catch ...

Modify the selection in one dropdown menu based on the selection in another dropdown menu using Angular 8

When I have two dropdowns, I aim to update the second dropdown with a matching JSON object based on the value selected in the first dropdown. JSON this.dropdownValues = { "mysql 8": { "flavor": [ "medium", ...

Error: The property '...' is not found in the ReactElement<any, any> type, but it is required in the type '{...}'

As a beginner in TypeScript, I am currently working on rendering a page by fetching data from getStaticProps. The code snippet I am using for this purpose is: import React, {FormEvent, useState} from "react"; import { InferGetStaticPropsType } fr ...

Could it be possible for TypeScript inference to directly infer the value and omit the key in the process?

class A { state: B } class B { something: C } class C { a: string; b: boolean; } type MagicType = ... const c: MagicType<A> c.state.a = "123" c.state.b = true; Is it possible to achieve the mentioned functionality without altering the exi ...

What specific characteristic of TypeScript's number data type or the Math.ceil() function is responsible for this calculation mistake?

Currently, I am working on a function in Typescript that is supposed to generate a unique number each time it runs. However, there seems to be a problem with the arithmetic as the results are not always correct. Upon further examination of the code below, ...

Limiting the image width of ngx-image-cropper to the popup container dimensions

I am currently working with a popup that contains an image cropper using ngx-image-cropper (https://www.npmjs.com/package/ngx-image-cropper) <div mat-dialog-container> <image-cropper [imageBase64]="imageFile" [mainta ...

Declaration files for Typescript ESLint configurations

I've been researching this issue online, but I haven't been able to find any solutions. It could be because I'm not entirely sure what's causing the problem. What I'm trying to do is set a global value on the Node.js global object ...

The imported variables are of a union type

In my nextjs project, I developed a customized hook to determine if a specific container is within the viewport using the intersection observer. Here's the code for the custom hook: import { useEffect, useRef, useState } from 'react'; cons ...

Using TypeScript: creating functions without defining an interface

Can function props be used without an interface? I have a function with the following properties: from - HTML Element to - HTML Element coords - Array [2, 2] export const adjustElements = ({ from, to, coords }) => { let to_rect = to.getBoundingC ...

Select characteristics with designated attribute types

Is there a way to create a type that selects only properties from an object whose values match a specific type? For example: type PickOfValue<T, V extends T[keyof T]> = { [P in keyof (key-picking magic?)]: T[P]; }; I am looking for a solution w ...

Unlocking the TypeScript UMD global type definition: A step-by-step guide

I have incorporated three@^0.103.0 into my project, along with its own type definitions. Within my project's src/global.d.ts, I have the following: import * as _THREE from 'three' declare global { const THREE: typeof _THREE } Additio ...

Guide on creating a universal template from a collection of interfaces

Two interfaces, AllTypes type: interface A { // ... } interface B { // ... } type AllTypes = A | B; How can I utilize generics to ensure that a function's argument is an object with either interface A or B? // pseudocode function test<T ...

Dealing with the possibility of an empty array when accessing elements by index in Typescript

What is the best way to handle accessing elements by index in an array in Typescript when the array can be empty, resulting in potentially undefined elements? I am developing a simple game using React and Typescript where I have a variable named game whic ...

Set an interface to null within Angular 4

I've created an interface in Angular 4 called StatusDetail: interface StatusDetail { statusName: string, name: string } Next, I assigned some values to it within an Angular component: //Angular Component export class EditComponent implemen ...

Angular 2 Material Primary Focus

Struggling with altering the foreground color in Angular 2 material? Specifically, the text in the toolbar displays as black. I attempted to adjust it using the following styles: @import '~@angular/material/theming'; $primary: mat-palette($mat- ...

Dealing with ViewChild and Stepper ExpressionChangedAfterItHasBeenCheckedError in Angular 8

I am currently facing an issue while using ViewChild to access a child component's attribute in the Stepper [completed] property. The problem is related to the "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ...

After refreshing the page, the JWT token is no longer available

I've recently launched my app on Heroku at this link: Give it a try yourself to witness the issue. Everything seems to be working fine until you refresh the website. The JWT token is stored in LocalStorage but gets lost from the header upon page re ...

Find keys in an array based on a specified value

I need to retrieve an array of keys from an object that match a specified value ...