The execution of setRequestHeader in ng-file-upload has encountered an error

Having encountered an issue with ng-file-upload, I have implemented a code similar to the GitHub example. However, upon attempting to upload a file, I am confronted with the following error:

Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function (xhr) {
          if (!xhr || !(xhr instanceof XMLHttpRequest)) return;
          config.__XHR = xhr;
          if (config.xhrFn) config.xhrFn(xhr);
          xhr.upload.addEventListener('progress', function (e) {
            e.config = config;
            notifyProgress(getNotifyEvent(e));
          }, false);
          //fix for firefox not firing upload progress end, also IE8-9
          xhr.upload.addEventListener('load', function (e) {
            if (e.lengthComputable) {
              e.config = config;
              notifyProgress(getNotifyEvent(e));
            }
          }, false);
        }' is not a valid HTTP header field value.

I am utilizing the latest version of ng-file-upload alongside angular 1.5.0 (tested with 1.4.7 as well, same error persists). Adhering closely to the example, my HTML directive with upload appears as follows:

<div>
    <label>{{field.label}} <span ng-if="field.required" class="text-red">*</span></label>

    <p ng-show="field.help">
      {{field.help}}
    </p>

    Drop File:
    <div ngf-drop ngf-select ng-model="files" class="drop-box"
         ngf-drag-over-class="'dragover'" ngf-multiple="true" ngf-allow-dir="true"
         accept="image/*,application/pdf"
         ngf-pattern="'image/*,application/pdf'">Drop pdfs or images here or click to upload</div>
    <div ngf-no-file-drop>File Drag/Drop is not supported for this browser</div>

    Files:
    <ul>
        <li ng-repeat="f in files">{{f.name}} {{f.$error}} {{f.$errorParam}}</li>
    </ul>
    
    Upload Log:
    <pre>{{log}}</pre>
</div>

Incorporating my own upload service:

scope.$watch('files', function() {
                scope.upload(scope.files);
            });

            scope.log = '';

            scope.upload = function(files) {
                if (files && files.length) {
                    for (var i = 0; i < files.length; i++) {
                        var file = files[i];
                        if (!file.$error) {
                            Upload.upload({
                                url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
                                data: {
                                    file: file
                                }
                            }).progress(function(evt) {
                                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                                scope.log = 'progress: ' + progressPercentage + '% ' +
                                    evt.config.data.file.name + '\n' + scope.log;
                            }).success(function(data, status, headers, config) {
                                $timeout(function() {
                                    scope.log = 'file: ' + config.data.file.name + ', Response: ' + JSON.stringify(data) + '\n' + scope.log;
                                });
                            });
                        }
                    }
                }
            };

Additionally, including a shim before angular.js:

<script type="text/javascript" src="vendor/jquery/dist/jquery.min.js"></script>

<script type="text/javascript" src="vendor/datatables/media/js/jquery.dataTables.min.js"></script>

<script type="text/javascript" src="vendor/ng-file-upload/ng-file-upload-shim.min.js"></script>

<script type="text/javascript" src="vendor/angular/angular.js"></script>

...

<script type="text/javascript" src="vendor/ng-file-upload/ng-file-upload.js"></script>

<script type="text/javascript" src="src/app/app.js"></script>

<script type="text/javascript" src="src/app/client/client.js"></script>

Seeking assistance in identifying the potential cause of this error.

Answer №1

After investigation, it appears that the issue stemmed from the PACE.js library used for JavaScript progress bars in XHR requests. The recommended solutions are either removing the library entirely or implementing a patch outlined in this GitHub link: https://github.com/danialfarid/ng-file-upload/issues/98

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

The proper event listener for a Javascript input field to trigger an Ajax query

My goal is to have the province, town, and barangay fields automatically filled in after a member is selected. I also want to add another listener for the province field to initiate another query for shipping methods. However, my current code seems to be i ...

local individuals and local residents (duplicate) dispatched from the server

Upon analyzing my server's response, I have observed a duplicate of my locals within the locals object. Here is an example: Object { settings: "4.2", env: "development", utils: true, pretty: true, _locals: { settings: ...

AngularJS mouse event is triggered repetitively within a loop

My goal is to activate the function setHighlight when a li element is hovered over with a mouse. The ng-mouseover event is employed within an ng-repeat loop. The problem arises when hovering over a li element: the ng-mouseover event gets triggered multipl ...

Guarantee: exclude all successful and unsuccessful responses and only carry out the .finally method

I have a function named request: function request (endpoint) { return axios.request(endpoint).then(api.onSuccess).catch(api.onError) } api.onSuccess: onSuccess (response) { let breakChain = false ... adding some logic here ... return break ...

The Angular @HostListener beforeunload Event is a powerful way to handle

I've implemented the following code snippet in my main app.component.ts file within my Angular 17 project: @HostListener("window:beforeunload", ["$event"]) onTabClose($event: BeforeUnloadEvent) { $event.preventDefault(); ...

Tips for updating data in the AngularJS Smart TableWould you like to learn

I'm still getting the hang of Java Script, so please bear with me if this question is a bit basic. Does anyone know how to edit rows in tables using Smart-Table with AngularJS? I can't seem to find any tutorials that match the new version of Sma ...

Guide on implementing ES6 script's template literals

I've been tackling a template literal question on hackerrank. It's working smoothly on my local IDE, but encountering an error on the Hackerrank IDE. Here's the code to add two numbers and print the result using a template literal: const sum ...

Exploring the capability of utilizing undefined properties within the ng model in Angular

Received a code and noticed that the properties like user.name or user.email are being used in ng-model without being declared in the controller. How is it still functioning? How can we send user information to the controller function using ng-click="upda ...

Incorporating multiple colors into a div with jQuery: A guide

I am looking for a way to create a legend by merging a specified number of colors within a div. I came across this useful Sample code that I have referenced. $.each(Array(50), function() { $("<div>").appendTo(document.body); }); var divs = $(&a ...

Is there a way to retrieve the value of elements that are deeply nested within multiple objects and arrays?

When making an API call to retrieve data from the Google Distance Matrix API, I store that information in my Redux store within a React application. The returned data object is structured as follows: Object { "destination_addresses": Array [ "21 Fo ...

Issues arise when jQuery functions do not execute as expected within an "if" statement following

Recently, I delved into the realm of AJAX and embarked on a journey to learn its intricacies. Prior to seeking assistance here, I diligently scoured through past queries, such as this, but to no avail. Here is an excerpt from my code: $('.del'). ...

Tips on incorporating a past random selection into an RPG lifepath generator's current random selection process

Currently, I am in the process of creating a JavaScript version of a basic "life path generator" commonly seen in pen and paper RPGs. The generators I have been working on are inspired by the structure provided at this link: However, I am encountering dif ...

The Codeigniter submission form using jQuery ajax() encountered an error

Can someone please help me out? I've gone through numerous tutorials on CodeIgniter from various websites, including Stack Overflow, but haven't found a solution to my problem. Guys, can you please teach me? This is the jQuery ajax function code ...

How to toggle a boolean variable in AngularJS when transitioning between states

Just getting started with AngularJS and trying to figure out how to tackle this issue. I have set up the following route: name: "Tracker", url: "/tracker/:period", and I have 3 distinct states for which I've created 3 separate functions to facilit ...

Differences between Global and Local Variables in Middleware Development

While exploring ways to manage globally used data in my research, I stumbled upon this question: See 2. Answer After integrating the suggested approach into my codebase, I encountered a problem that I would like to discuss and seek help for. I created a ...

What could be causing the appearance of a Firefox error message during the execution of a Protractor test?

Currently, I am conducting end-to-end testing on an AngularJS application using Protractor. Every time I execute a spec, Firefox launches and closes with a particular message appearing: After that, Firefox starts working properly and the specs run smooth ...

Optimal strategy for creating a kiosk application that features dynamically changing views based on a timer

Currently, I am developing an app with 6 different views, each paired with its own controller. I am interested in implementing an automatic view switch after a certain number of seconds, looping back to the beginning once it reaches the end. In addition, ...

Ensure the validation of multiple form fields in a single function

I've implemented code to validate a form field as you input values, changing the border color to red if invalid or green if valid: function FormValidation(){ var fn=document.getElementById("firstName").value; if(fn == ""){ document.ge ...

How come my button is initiating automatically instead of manually?

Working on developing an API using Angular 2 with the Janus media server has brought up an issue regarding the start button. When running Janus, the button initiates automatically instead of manually. The following function was implemented for this purpos ...

Stateprovider view templates in AngularJS

WikiApp.config(function config($stateProvider, $urlRouterProvider) { $stateProvider .state('revision', { url: '/wiki', views: { "main": { controller: 'ListCtrl', ...