Skip to content

Commit

Permalink
file data upload fix
Browse files Browse the repository at this point in the history
  • Loading branch information
teeman committed Sep 25, 2011
1 parent cf0f82a commit 4f070b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
6 changes: 4 additions & 2 deletions AFNetworking/AFHTTPClient.h
Expand Up @@ -37,7 +37,7 @@
}

/**
The url used as the base for paths specified in methods such as `getPath:parameters:success:failure`
The url used as the base for paths specified in methods such as `getPath:parameteres:success:failure`
*/
@property (readonly, nonatomic, retain) NSURL *baseURL;

Expand All @@ -49,7 +49,7 @@
/**
The operation queue which manages operations enqueued by the HTTP client.
*/
@property (readonly, nonatomic, retain) NSOperationQueue *operationQueue;
@property (readonly, nonatomic, retain) NSOperationQueue *operationQueue;;

///---------------------------------------------
/// @name Creating and Initializing HTTP Clients
Expand Down Expand Up @@ -267,6 +267,8 @@
*/
- (void)appendPartWithFile:(NSURL *)fileURL mimeType:(NSString *)mimeType fileName:(NSString *)fileName;

- (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name;

/**
Appends encoded data to the form data.
Expand Down
25 changes: 16 additions & 9 deletions AFNetworking/AFHTTPClient.m
Expand Up @@ -27,11 +27,11 @@
static NSString * const kAFMultipartFormBoundary = @"Boundary+0xAbCdEfGbOuNdArY";

static NSString * AFMultipartFormEncapsulationBoundary() {
return [NSString stringWithFormat:@"--%@", kAFMultipartFormBoundary];
return [NSString stringWithFormat:@"%@--%@%@", kAFMultipartFormLineDelimiter, kAFMultipartFormBoundary, kAFMultipartFormLineDelimiter];
}

static NSString * AFMultipartFormFinalBoundary() {
return [NSString stringWithFormat:@"--%@--", kAFMultipartFormBoundary];
return [NSString stringWithFormat:@"%@--%@--", kAFMultipartFormLineDelimiter, kAFMultipartFormBoundary];
}

@interface AFMultipartFormDataProxy : NSObject <AFMultipartFormDataProxy> {
Expand Down Expand Up @@ -305,21 +305,17 @@ - (void)dealloc {
- (NSData *)data {
NSMutableData *finalizedData = [NSMutableData dataWithData:self.mutableData];
[finalizedData appendData:[AFMultipartFormFinalBoundary() dataUsingEncoding:self.stringEncoding]];

return finalizedData;
}

#pragma mark - AFMultipartFormDataProxy

- (void)appendPartWithHeaders:(NSDictionary *)headers body:(NSData *)body {
if ([self.mutableData length] > 0) {
[self appendString:AFMultipartFormEncapsulationBoundary()];
[self appendBlankLine];
}

[self appendString:AFMultipartFormEncapsulationBoundary()];

for (NSString *field in [headers allKeys]) {
[self appendString:[NSString stringWithFormat:@"%@: %@", field, [headers valueForKey:field]]];
[self appendBlankLine];
[self appendString:[NSString stringWithFormat:@"%@: %@%@", field, [headers valueForKey:field], kAFMultipartFormLineDelimiter]];
}

[self appendBlankLine];
Expand Down Expand Up @@ -351,6 +347,17 @@ - (void)appendPartWithFile:(NSURL *)fileURL mimeType:(NSString *)mimeType fileNa
[self appendPartWithHeaders:mutableHeaders body:data];
}

- (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name {

NSString *fileName = [[NSString stringWithFormat:@"%d", [[NSDate date] hash]] stringByAppendingPathExtension:[mimeType lastPathComponent]];

NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"file; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
[mutableHeaders setValue:mimeType forKey:@"Content-Type"];

[self appendPartWithHeaders:mutableHeaders body:data];
}

- (void)appendData:(NSData *)data {
[self.mutableData appendData:data];
}
Expand Down

0 comments on commit 4f070b2

Please sign in to comment.