Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5b0d068

Browse files
committed
fix($http): Do not serialize File object
1 parent 230f29d commit 5b0d068

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Angular.js

+5
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ function isScope(obj) {
417417
}
418418

419419

420+
function isFile(obj) {
421+
return toString.apply(obj) === '[object File]';
422+
}
423+
424+
420425
function isBoolean(value) {return typeof value == $boolean;}
421426
function isTextNode(node) {return nodeName_(node) == '#text';}
422427

src/service/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function $HttpProvider() {
102102

103103
// transform outgoing request data
104104
transformRequest: function(d) {
105-
return isObject(d) ? toJson(d) : d;
105+
return isObject(d) && !isFile(d) ? toJson(d) : d;
106106
},
107107

108108
// default headers

test/service/httpSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,25 @@ describe('$http', function() {
564564
$httpBackend.expect('POST', '/url', 'string-data').respond('');
565565
$http({method: 'POST', url: '/url', data: 'string-data'});
566566
});
567+
568+
569+
it('should ignore File objects', function() {
570+
var file = {
571+
some: true,
572+
// $httpBackend compares toJson values by default,
573+
// we need to be sure it's not serialized into json string
574+
test: function(actualValue) {
575+
return this === actualValue;
576+
}
577+
};
578+
579+
// I'm really sorry for doing this :-D
580+
// Unfortunatelly I don't know how to trick toString.apply(obj) comparison
581+
spyOn(window, 'isFile').andReturn(true);
582+
583+
$httpBackend.expect('POST', '/some', file).respond('');
584+
$http({method: 'POST', url: '/some', data: file});
585+
});
567586
});
568587

569588

0 commit comments

Comments
 (0)