From 9a91afe3e25e252b3145815799dbbe7dde3d8008 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Mon, 7 Nov 2011 12:16:42 -0600 Subject: [PATCH] [Issue #98] Adding AFHTTPClient -appendPartWithFileURL:name:error: Changing -appendPartWithFileData:mimeType:name: to -appendPartWithFileData:name:fileName:mimeType: --- AFNetworking/AFHTTPClient.h | 20 ++++++++++++++++---- AFNetworking/AFHTTPClient.m | 24 +++++++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/AFNetworking/AFHTTPClient.h b/AFNetworking/AFHTTPClient.h index 18bd29c..464f797 100644 --- a/AFNetworking/AFHTTPClient.h +++ b/AFNetworking/AFHTTPClient.h @@ -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 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 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`. - @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. */ -- (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name; +- (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 and MIME type for this data in the form will be automatically generated, using `NSURLResponse` `-MIMEType` and `-suggestedFilename`. + */ +- (void)appendPartWithFileURL:(NSURL *)fileURL name:(NSString *)name error:(NSError **)error; /** Appends encoded data to the form data. diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index ba1f938..84d76df 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -454,9 +454,7 @@ static inline NSString * AFMultipartFormFinalBoundary() { [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"]; @@ -464,6 +462,26 @@ static inline NSString * AFMultipartFormFinalBoundary() { [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]; }