FormData can append only string or object with uri

Summary:
I fix FormData type checking.

Simple test case:
```js
var fd = new FormData();

// JS Error.
// Because all non-string values threaded as "blob"
fd.append('number', 1);
```
Closes https://github.com/facebook/react-native/pull/4390

Reviewed By: svcscm

Differential Revision: D2818377

Pulled By: nicklockwood

fb-gh-sync-id: 8b3f27476af21c5fd65b844034579372172ea73c
This commit is contained in:
Aleksei Androsov 2016-01-12 03:24:42 -08:00 committed by facebook-github-bot-4
parent 43dcdaffe2
commit 5061fde317
1 changed files with 10 additions and 9 deletions

View File

@ -71,21 +71,22 @@ class FormData {
/* $FlowIssue(>=0.20.1) #9463928 */ /* $FlowIssue(>=0.20.1) #9463928 */
var headers: Headers = {'content-disposition': contentDisposition}; var headers: Headers = {'content-disposition': contentDisposition};
if (typeof value === 'string') {
return {string: value, headers, fieldName: name};
}
// The body part is a "blob", which in React Native just means // The body part is a "blob", which in React Native just means
// an object with a `uri` attribute. Optionally, it can also // an object with a `uri` attribute. Optionally, it can also
// have a `name` and `type` attribute to specify filename and // have a `name` and `type` attribute to specify filename and
// content type (cf. web Blob interface.) // content type (cf. web Blob interface.)
if (typeof value.name === 'string') { if (typeof value === 'object') {
headers['content-disposition'] += '; filename="' + value.name + '"'; if (typeof value.name === 'string') {
headers['content-disposition'] += '; filename="' + value.name + '"';
}
if (typeof value.type === 'string') {
headers['content-type'] = value.type;
}
return {...value, headers, fieldName: name};
} }
if (typeof value.type === 'string') { // Cast to string all other values
headers['content-type'] = value.type; return {string: String(value), headers, fieldName: name};
}
return {...value, headers, fieldName: name};
}); });
} }
} }