[Issue #98] Adding AFHTTPClient -appendPartWithFileURL:name:error:

Changing -appendPartWithFileData:mimeType:name: to -appendPartWithFileData:name:fileName:mimeType:
This commit is contained in:
Mattt Thompson 2011-11-07 12:16:42 -06:00
parent afbd799e3b
commit 9a91afe3e2
2 changed files with 37 additions and 7 deletions

View file

@ -355,15 +355,27 @@ typedef enum {
- (void)appendPartWithFormData:(NSData *)data name:(NSString *)name; - (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 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 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. Appends encoded data to the form data.

View file

@ -454,9 +454,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
[self appendPartWithHeaders:mutableHeaders body:data]; [self appendPartWithHeaders:mutableHeaders body:data];
} }
- (void)appendPartWithFileData:(NSData *)data mimeType:(NSString *)mimeType name:(NSString *)name { - (void)appendPartWithFileData:(NSData *)data name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType {
NSString *fileName = [[NSString stringWithFormat:@"%@-%d", name, [[NSDate date] hash]] stringByAppendingPathExtension:[mimeType lastPathComponent]];
NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary]; NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
[mutableHeaders setValue:[NSString stringWithFormat:@"file; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"]; [mutableHeaders setValue:[NSString stringWithFormat:@"file; name=\"%@\"; filename=\"%@\"", name, fileName] forKey:@"Content-Disposition"];
[mutableHeaders setValue:mimeType forKey:@"Content-Type"]; [mutableHeaders setValue:mimeType forKey:@"Content-Type"];
@ -464,6 +462,26 @@ static inline NSString * AFMultipartFormFinalBoundary() {
[self appendPartWithHeaders:mutableHeaders body:data]; [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 { - (void)appendData:(NSData *)data {
[self.mutableData appendData:data]; [self.mutableData appendData:data];
} }