Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

improvement for fieldName #767

Closed
wants to merge 1 commit into from

2 participants

@XBeg9

I have need in custom fieldName and filename option in addFile function. This fix has improved generatePostBody

@diederich

hm, looks like this breaks the internal logic of the _files array, if there is more than one file.

Currently the _files array seems to expect exactly 3 parts: (data, mimeType,fieldName). See the loop here, especially "i+3" https://github.com/XBeg9/three20/blob/670d765990ad189893d139866f2c7efb5c3dc176/src/Three20Network/Sources/TTURLRequest.m#L239

I guess this could be fixed by either adding always 4-parts (and thus an empty element if there's no fieldName), or e.g. by bundling the information in the array either in a custom object, or by using a dictionary.

what do you think?

@XBeg9

Yes, I just show you my "fix" :) I am new in iOS development

@diederich

Yeah, thanks for taking the time to do this!
Feel free to update this pull request with an adjusted fix!
(+1 for creating a new pull request, targeting the development branch :))

@XBeg9

ok, thanks:) have a nice day :)

@XBeg9 XBeg9 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 2, 2012
  1. Post body field name for file.

    XBeg9 authored
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 3 deletions.
  1. +23 −3 src/Three20Network/Sources/TTURLRequest.m
View
26 src/Three20Network/Sources/TTURLRequest.m
@@ -240,12 +240,20 @@ - (NSData*)generatePostBody {
NSData* data = [_files objectAtIndex:i];
NSString* mimeType = [_files objectAtIndex:i+1];
NSString* fileName = [_files objectAtIndex:i+2];
-
+ NSString* fieldName = [_files objectAtIndex:i+3];
+
[body appendData:[beginLine dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:
+ if([fieldName length])
+ [body appendData:[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",
- fileName, fileName]
+ fileName, fieldName]
dataUsingEncoding:_charsetForMultipart]];
+ else
+ [body appendData:[[NSString stringWithFormat:
+ @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",
+ fileName, fileName]
+ dataUsingEncoding:_charsetForMultipart]];
+
[body appendData:[[NSString stringWithFormat:@"Content-Length: %d\r\n", data.length]
dataUsingEncoding:_charsetForMultipart]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", mimeType]
@@ -351,6 +359,18 @@ - (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)f
[_files addObject:fileName];
}
+///////////////////////////////////////////////////////////////////////////////////////////////////
+- (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)fileName fieldName:(NSString*)fieldName {
+ if (!_files) {
+ _files = [[NSMutableArray alloc] init];
+ }
+
+ [_files addObject:data];
+ [_files addObject:mimeType];
+ [_files addObject:fileName];
+ [_files addObject:fieldName];
+}
+
///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)send {
Something went wrong with that request. Please try again.