Skip to content

Commit

Permalink
[Issue #98] Adding AFHTTPClient -appendPartWithFileURL:name:error:
Browse files Browse the repository at this point in the history
Changing -appendPartWithFileData:mimeType:name: to -appendPartWithFileData:name:fileName:mimeType:
  • Loading branch information
mattt committed Nov 7, 2011
1 parent afbd799 commit 9a91afe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
18 changes: 15 additions & 3 deletions AFNetworking/AFHTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,27 @@ typedef enum {
- (void)appendPartWithFormData:(NSData *)data name:(NSString *)name;

/**
Appends the HTTP header `Content-Disposition: file; filename=#{generated filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary.
Appends the HTTP header `Content-Disposition: file; filename=#{filename}; name=#{name}"` and `Content-Type: #{mimeType}`, followed by the encoded file data and the multipart form boundary.
@param data The data to be encoded and appended to the form data.
@param name The name to be associated with the specified data. This parameter must not be `nil`.
@param mimeType The MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be `nil`.
@param name The filename to be associated with the specified data. This parameter must not be `nil`.
*/
- (void)appendPartWithFileData:(NSData *)data name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType;


/**
Appends the HTTP header `Content-Disposition: file; filename=#{generated filename}; name=#{name}"` and `Content-Type: #{generated mimeType}`, followed by the encoded file data and the multipart form boundary.
@param fileURL The URL corresponding to the file whose content will be appended to the form.
@param name The name to be associated with the specified data. This parameter must not be `nil`.
@param error If an error occurs, upon return contains an `NSError` object that describes the problem.
@discussion The filename associated with this data in the form will be automatically generated using the parameter name specified and a unique timestamp-based hash.
@discussion The filename and MIME type for this data in the form will be automatically generated, using `NSURLResponse` `-MIMEType` and `-suggestedFilename`.
*/
- (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name;
- (void)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name error:(NSError **)error;

/**
Appends encoded data to the form data.
Expand Down
24 changes: 21 additions & 3 deletions AFNetworking/AFHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,34 @@ - (void)appendPartWithFormData:(NSData *)data name:(NSString *)name {
[self appendPartWithHeaders:mutableHeaders body:data];
}

- (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name {
NSString *fileName = [[NSString stringWithFormat:@"%@-%d", name, [[NSDate date] hash]] stringByAppendingPathExtension:[mimeType lastPathComponent]];

- (void)appendPartWithFileData:(NSData *)data name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType {
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)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name error:(NSError **)error {
if (![fileURL isFileURL]) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setValue:fileURL forKey:NSURLErrorFailingURLErrorKey];
[userInfo setValue:NSLocalizedString(@"Expected URL to be a file URL", nil) forKey:NSLocalizedFailureReasonErrorKey];
*error = [[[NSError alloc] initWithDomain:AFNetworkingErrorDomain code:NSURLErrorBadURL userInfo:userInfo] autorelease];
return;
}

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL];
[request setCachePolicy:NSURLCacheStorageNotAllowed];

NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

if (response && !error) {
[self appendPartWithFileData:data name:name fileName:[response suggestedFilename] mimeType:[response MIMEType]];
}
}

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

0 comments on commit 9a91afe

Please sign in to comment.