From 5b0d0683584e304db30462f3448d9f090120c444 Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Sat, 25 Feb 2012 18:49:54 -0800 Subject: [PATCH] fix($http): Do not serialize File object --- src/Angular.js | 5 +++++ src/service/http.js | 2 +- test/service/httpSpec.js | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index 87c10e23ae3b..fec866f51e35 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -417,6 +417,11 @@ function isScope(obj) { } +function isFile(obj) { + return toString.apply(obj) === '[object File]'; +} + + function isBoolean(value) {return typeof value == $boolean;} function isTextNode(node) {return nodeName_(node) == '#text';} diff --git a/src/service/http.js b/src/service/http.js index 93d44eac889a..afaae2de2848 100644 --- a/src/service/http.js +++ b/src/service/http.js @@ -102,7 +102,7 @@ function $HttpProvider() { // transform outgoing request data transformRequest: function(d) { - return isObject(d) ? toJson(d) : d; + return isObject(d) && !isFile(d) ? toJson(d) : d; }, // default headers diff --git a/test/service/httpSpec.js b/test/service/httpSpec.js index ad376b5fe362..4c8471d8fdf2 100644 --- a/test/service/httpSpec.js +++ b/test/service/httpSpec.js @@ -564,6 +564,25 @@ describe('$http', function() { $httpBackend.expect('POST', '/url', 'string-data').respond(''); $http({method: 'POST', url: '/url', data: 'string-data'}); }); + + + it('should ignore File objects', function() { + var file = { + some: true, + // $httpBackend compares toJson values by default, + // we need to be sure it's not serialized into json string + test: function(actualValue) { + return this === actualValue; + } + }; + + // I'm really sorry for doing this :-D + // Unfortunatelly I don't know how to trick toString.apply(obj) comparison + spyOn(window, 'isFile').andReturn(true); + + $httpBackend.expect('POST', '/some', file).respond(''); + $http({method: 'POST', url: '/some', data: file}); + }); });