question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

angular-file-upload-shim does not work correctly on Chrome - Failed to execute 'setRequestHeader' on 'XMLHttpRequest'

See original GitHub issue

When I try to upload a file, I get this error:

Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'function (xhr) {
            if (!xhr) return;
            config.__XHR = xhr;
            config.xhrFn && config.xhrFn(xhr);
            xhr.upload.addEventListener('progress', function(e) {
                e.config = config;
                deferred.notify ? deferred.notify(e) : promise.progress_fn && $timeout(function(){promise.progress_fn(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;
                    deferred.notify ? deferred.notify(e) : promise.progress_fn && $timeout(function(){promise.progress_fn(e)});
                }
            }, false);
        }' is not a valid HTTP header field value.

What I figured out is that $upload sets a fake header called __setXHR_ and angular-file-upload-shim patches some methods to manage the fake header here:

patchXHR('setRequestHeader', function(orig) {
    return function(header, value) {
        if (header === '__setXHR_') {
            initializeUploadListener(this);
            var val = value(this);
            // fix for angular < 1.2.0
            if (val instanceof Function) {
                val(this);
            }
        } else {
            this.__requestHeaders = this.__requestHeaders || {};
            this.__requestHeaders[header] = value;
            orig.apply(this, arguments);
        }
    }
});

The problem is that angular-file-upload-shim conditionally executes most of its code:

 if ((window.XMLHttpRequest && !window.FormData) || (window.FileAPI && FileAPI.forceLoad))

And at least in my browser (Chrome 40.0.2214.93 for Linux), window.FormData is set, and window.FileAPI is not, so the condition isn’t met.

In summary:

  • The fake header is set
  • The patched XHR function that would process the fake header is not set.
  • The core javascript libraries cannot process the fake header and everything stops working

Versions:

  • angular 1.3.12
  • ng-file-upload 3.0.7

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
TheFabiocommented, May 7, 2016

Loading “ng-file-upload.js” before loading “pace.js” fixed the issue for me.

4reactions
pradtcommented, Nov 2, 2016

Hi, how did you guys work out that it is was Pace.js ? I’m not using pace.js but I’m receiving this error, and trying to work out what is the conflict.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed to execute 'setRequestHeader' on 'XMLHttpRequest ...
I had faced this problem. Please make sure that in your code header name doesn't contain space. i.e. 'designId ' is invalid, as...
Read more >
Tm1-ui-upload libs.3.0.1.js:9576 DOMException: Failed to ...
Issue: angular-file-upload-shim does not work correctly on Chrome - Failed to execute 'setRequestHeader' on 'XMLHttpRequest'.
Read more >
'Authorization: Bearer ' is not a valid HTTP header field name
Chrome is giving the Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Authorization: Bearer ' is not a valid HTTP header field name ...
Read more >
Fix Chrome update problems & failed updates - Google Support
If you're having problems updating Chrome on your computer, you might see: Update failed: Updates are disabled by administrator Update failed (Error: 3...
Read more >
Failed to execute 'setRequestHeader' on 'XMLHttpRequest'
... in angular but give an error Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'Access- Control - Allow - Methods' is not a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found