Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular 1.3.0 RC5 breaks angular-file-upload (RC4 worked fine) #272

Closed
hoeni opened this issue Oct 15, 2014 · 18 comments
Closed

Angular 1.3.0 RC5 breaks angular-file-upload (RC4 worked fine) #272

hoeni opened this issue Oct 15, 2014 · 18 comments

Comments

@hoeni
Copy link

hoeni commented Oct 15, 2014

Hi,

until AngularJS 1.3.0-rc.4 the file upload was working perfect for me. Since AngularJS 1.3.0-rc.5 and in the brand new 1.3.0 release it's broken:

My callbacks for onSuccessItem and onCompleteItem are not called anymore, instead I get a Uncaught TypeError: undefined is not a function message on the console, tracing back to a defaultHttpResponseTransform method in Angular's $HttpProvider.

I'd really love to use this great module with Angular 1.3...

@hoeni
Copy link
Author

hoeni commented Oct 15, 2014

Seems to be caused by angular/angular.js#2973 / angular/angular.js@7b6c1d0

@caitp
Copy link

caitp commented Oct 15, 2014

Please post the entire stack trace so we can see how angular/angular.js@7b6c1d0 is implicated, if it is at all

@caitp
Copy link

caitp commented Oct 15, 2014

Better yet a reproduction

@hoeni
Copy link
Author

hoeni commented Oct 15, 2014

Uncaught TypeError: undefined is not a function angular.js:8569
defaultHttpResponseTransform angular.js:8569
(anonymous function) angular-file-upload.js:1319
forEach angular.js:335
g._transformResponse angular-file-upload.js:1319
c.onload angular-file-upload.js:1319

@caitp
Copy link

caitp commented Oct 15, 2014

https://github.com/nervgh/angular-file-upload/blob/master/angular-file-upload.js#L408 is calling the function without headers --- it probably shouldnt be calling transform functions at all, $http should be doing that for you. Unless you can emulate the headers object, don't do it

@hoeni
Copy link
Author

hoeni commented Oct 15, 2014

@caitp A fix would be simple to make Angular robust against that (probably wrong) behaviour (hoeni/angular.js@40a40b4) but I'm struggling getting the pull request right (never done that before...)

@caitp
Copy link

caitp commented Oct 15, 2014

A fix would be to call transformResponse functions with data that they're expected to receive --- even if Angular were robust against this, that wouldn't apply to custom transformers. If you want to call transformResponse, call them with data they're documented to have, or don't call them at all. Period. It's that simple.

@caitp
Copy link

caitp commented Oct 15, 2014

This is a bug in angular-file-upload, so that's a relief =)

@hoeni
Copy link
Author

hoeni commented Oct 15, 2014

@caitp So thanks for your investigation!

@mikhail-eremin
Copy link

I had this problem too, after update to angular 1.3 uploader stopped working with same error (
I've tried solution from caitp, and i can approve it works, thanks!
And thanks for great module, using it already for a year

@aureliusm
Copy link

So what is the solution? :) Can't seem to understand it..

@caitp
Copy link

caitp commented Oct 16, 2014

Pass the header getter function

@mefu
Copy link

mefu commented Oct 17, 2014

Can you exactly say what to do for dumbs like me ?

@caitp
Copy link

caitp commented Oct 17, 2014

In pseudo code:

Function transformData(response):
    headersFn = parseHeadersAndReturnGetterFn(response.responseHeaders)
    data = response.data
    listOfFns = isList(fns) ? fns : [fns]

    for fn in fns:
        if IsFunction(fn):
            data = fn(data, headersFn)

    return data

@caitp
Copy link

caitp commented Oct 17, 2014

so, the code from angular for creating the headersFn is this:

function headersGetter(headers) {
  var headersObj = isObject(headers) ? headers : undefined;

  return function(name) {
    if (!headersObj) headersObj =  parseHeaders(headers);

    if (name) {
      return headersObj[lowercase(name)] || null;
    }

    return headersObj;
  };
}

And the code for parsing responseHeaders is this:

function parseHeaders(headers) {
  var parsed = {}, key, val, i;

  if (!headers) return parsed;

  forEach(headers.split('\n'), function(line) {
    i = line.indexOf(':');
    key = lowercase(trim(line.substr(0, i)));
    val = trim(line.substr(i + 1));

    if (key) {
      parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
    }
  });

  return parsed;
}

where headers is the result of xhr.getAllResponseHeaders()

@aureliusm
Copy link

Is this fix going to be added in the main version? That would be great..

@nervgh
Copy link
Owner

nervgh commented Oct 21, 2014

Thanks! Fixed.

@nervgh nervgh closed this as completed Oct 21, 2014
@hoeni
Copy link
Author

hoeni commented Oct 21, 2014

Could you bump the depency in bower.json to allow angular 1.3 without warning?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants