@@ -151,9 +151,15 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
151
151
NSString *fieldName = options[@" field" ];
152
152
NSString *customUploadId = options[@" customUploadId" ];
153
153
NSDictionary *headers = options[@" headers" ];
154
+ NSDictionary *parameters = options[@" parameters" ];
154
155
155
156
@try {
156
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: uploadUrl]];
157
+ NSURL *requestUrl = [NSURL URLWithString: uploadUrl];
158
+ if (requestUrl == nil ) {
159
+ @throw @" Request cannot be nil" ;
160
+ }
161
+
162
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: requestUrl];
157
163
[request setHTTPMethod: method];
158
164
159
165
[headers enumerateKeysAndObjectsUsingBlock: ^(id _Nonnull key, id _Nonnull val, BOOL * _Nonnull stop) {
@@ -188,12 +194,17 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
188
194
NSString *uuidStr = [[NSUUID UUID ] UUIDString ];
189
195
[request setValue: [NSString stringWithFormat: @" multipart/form-data; boundary=%@ " , uuidStr] forHTTPHeaderField: @" Content-Type" ];
190
196
191
- NSData *httpBody = [self createBodyWithBoundary: uuidStr path: fileURI fieldName: fieldName];
197
+ NSData *httpBody = [self createBodyWithBoundary: uuidStr path: fileURI parameters: parameters fieldName: fieldName];
192
198
[request setHTTPBody: httpBody];
193
199
194
200
// I am sorry about warning, but Upload tasks from NSData are not supported in background sessions.
195
201
uploadTask = [[self urlSession ] uploadTaskWithRequest: request fromData: nil ];
196
202
} else {
203
+ if (parameters.count > 0 ) {
204
+ reject (@" RN Uploader" , @" Parameters supported only in multipart type" , nil );
205
+ return ;
206
+ }
207
+
197
208
uploadTask = [[self urlSession ] uploadTaskWithRequest: request fromFile: [NSURL URLWithString: fileURI]];
198
209
}
199
210
@@ -225,6 +236,7 @@ - (void)copyAssetToFile: (NSString *)assetUrl completionHandler: (void(^)(NSStri
225
236
226
237
- (NSData *)createBodyWithBoundary : (NSString *)boundary
227
238
path : (NSString *)path
239
+ parameters : (NSDictionary *)parameters
228
240
fieldName : (NSString *)fieldName {
229
241
230
242
NSMutableData *httpBody = [NSMutableData data ];
@@ -237,6 +249,12 @@ - (NSData *)createBodyWithBoundary:(NSString *)boundary
237
249
NSString *filename = [path lastPathComponent ];
238
250
NSString *mimetype = [self guessMIMETypeFromFileName: path];
239
251
252
+ [parameters enumerateKeysAndObjectsUsingBlock: ^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
253
+ [httpBody appendData: [[NSString stringWithFormat: @" --%@ \r\n " , boundary] dataUsingEncoding: NSUTF8StringEncoding]];
254
+ [httpBody appendData: [[NSString stringWithFormat: @" Content-Disposition: form-data; name=\" %@ \"\r\n\r\n " , parameterKey] dataUsingEncoding: NSUTF8StringEncoding]];
255
+ [httpBody appendData: [[NSString stringWithFormat: @" %@ \r\n " , parameterValue] dataUsingEncoding: NSUTF8StringEncoding]];
256
+ }];
257
+
240
258
[httpBody appendData: [[NSString stringWithFormat: @" --%@ \r\n " , boundary] dataUsingEncoding: NSUTF8StringEncoding]];
241
259
[httpBody appendData: [[NSString stringWithFormat: @" Content-Disposition: form-data; name=\" %@ \" ; filename=\" %@ \"\r\n " , fieldName, filename] dataUsingEncoding: NSUTF8StringEncoding]];
242
260
[httpBody appendData: [[NSString stringWithFormat: @" Content-Type: %@ \r\n\r\n " , mimetype] dataUsingEncoding: NSUTF8StringEncoding]];
@@ -253,6 +271,7 @@ - (NSURLSession *)urlSession {
253
271
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: BACKGROUND_SESSION_ID];
254
272
_urlSession = [NSURLSession sessionWithConfiguration: sessionConfiguration delegate: self delegateQueue: nil ];
255
273
}
274
+
256
275
return _urlSession;
257
276
}
258
277
0 commit comments