forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ReactNative] Allow uploading native files (e.g. photos) and FormData…
… via XMLHttpRequest
- Loading branch information
1 parent
f590a8b
commit f4bf80f
Showing
13 changed files
with
512 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule FormData | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
type FormDataValue = any; | ||
type FormDataPart = [string, FormDataValue]; | ||
|
||
/** | ||
* Polyfill for XMLHttpRequest2 FormData API, allowing multipart POST requests | ||
* with mixed data (string, native files) to be submitted via XMLHttpRequest. | ||
*/ | ||
class FormData { | ||
_parts: Array<FormDataPart>; | ||
_partsByKey: {[key: string]: FormDataPart}; | ||
|
||
constructor() { | ||
this._parts = []; | ||
this._partsByKey = {}; | ||
} | ||
|
||
append(key: string, value: FormDataValue) { | ||
var parts = this._partsByKey[key]; | ||
if (parts) { | ||
// It's a bit unclear what the behaviour should be in this case. | ||
// The XMLHttpRequest spec doesn't specify it, while MDN says that | ||
// the any new values should appended to existing values. We're not | ||
// doing that for now -- it's tedious and doesn't seem worth the effort. | ||
parts[1] = value; | ||
return; | ||
} | ||
parts = [key, value]; | ||
this._parts.push(parts); | ||
this._partsByKey[key] = parts; | ||
} | ||
|
||
getParts(): Array<FormDataValue> { | ||
return this._parts.map(([name, value]) => { | ||
if (typeof value === 'string') { | ||
return { | ||
string: value, | ||
headers: { | ||
'content-disposition': 'form-data; name="' + name + '"', | ||
}, | ||
}; | ||
} | ||
var contentDisposition = 'form-data; name="' + name + '"'; | ||
if (typeof value.name === 'string') { | ||
contentDisposition += '; filename="' + value.name + '"'; | ||
} | ||
return { | ||
...value, | ||
headers: {'content-disposition': contentDisposition}, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = FormData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTBridgeModule.h" | ||
|
||
@protocol RCTDataQueryExecutor <NSObject> | ||
|
||
- (void)addQuery:(NSDictionary *)query responseSender:(RCTResponseSenderBlock)responseSender; | ||
|
||
@optional | ||
|
||
@property (nonatomic, weak) RCTBridge *bridge; | ||
@property (nonatomic, assign) BOOL sendIncrementalUpdates; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "RCTDataQuery.h" | ||
|
||
@interface RCTHTTPQueryExecutor : NSObject <RCTDataQueryExecutor> | ||
|
||
+ (instancetype)sharedInstance; | ||
|
||
/** | ||
* Process the 'data' part of an HTTP query. | ||
* | ||
* 'data' can be a JSON value of the following forms: | ||
* | ||
* - {"string": "..."}: a simple JS string that will be UTF-8 encoded and sent as the body | ||
* | ||
* - {"uri": "some-uri://..."}: reference to a system resource, e.g. an image in the asset library | ||
* | ||
* - {"formData": [...]}: list of data payloads that will be combined into a multipart/form-data request | ||
* | ||
* If successful, the callback be called with a result dictionary containing the following (optional) keys: | ||
* | ||
* - @"body" (NSData): the body of the request | ||
* | ||
* - @"contentType" (NSString): the content type header of the request | ||
* | ||
*/ | ||
+ (void)processDataForHTTPQuery:(NSDictionary *)data | ||
callback:(void (^)(NSError *error, NSDictionary *result))callback; | ||
|
||
@end |
Oops, something went wrong.