Committed by
Ben Copsey
MIME type defining feature for ASIFormDataRequest
Showing
2 changed files
with
28 additions
and
17 deletions
@@ -30,4 +30,7 @@ | @@ -30,4 +30,7 @@ | ||
30 | // Add the contents of an NSData object to the request | 30 | // Add the contents of an NSData object to the request |
31 | - (void)setData:(NSData *)data forKey:(NSString *)key; | 31 | - (void)setData:(NSData *)data forKey:(NSString *)key; |
32 | 32 | ||
33 | +// Allows to specify content type of the file | ||
34 | +- (void)setFileDataContainerObject:(id)data fileName:(NSString *)fileName contentType:(NSString *)contentType forKey:(NSString *)key; | ||
35 | + | ||
33 | @end | 36 | @end |
@@ -46,20 +46,26 @@ | @@ -46,20 +46,26 @@ | ||
46 | 46 | ||
47 | - (void)setFile:(NSString *)filePath forKey:(NSString *)key | 47 | - (void)setFile:(NSString *)filePath forKey:(NSString *)key |
48 | { | 48 | { |
49 | - if (!fileData) { | 49 | + [self setFileDataContainerObject:filePath fileName:@"file" contentType:@"application/octet-stream" forKey:key]; |
50 | - fileData = [[NSMutableDictionary alloc] init]; | ||
51 | - } | ||
52 | - [fileData setValue:filePath forKey:key]; | ||
53 | - [self setRequestMethod:@"POST"]; | ||
54 | } | 50 | } |
55 | 51 | ||
56 | - (void)setData:(NSData *)data forKey:(NSString *)key | 52 | - (void)setData:(NSData *)data forKey:(NSString *)key |
57 | { | 53 | { |
54 | + [self setFileDataContainerObject:data fileName:@"file" contentType:@"application/octet-stream" forKey:key]; | ||
55 | +} | ||
56 | + | ||
57 | +- (void)setFileDataContainerObject:(id)data | ||
58 | + fileName:(NSString *)fileName | ||
59 | + contentType:(NSString *)contentType | ||
60 | + forKey:(NSString *)key { | ||
58 | if (!fileData) { | 61 | if (!fileData) { |
59 | - fileData = [[NSMutableDictionary alloc] init]; | 62 | + fileData = [[NSMutableDictionary alloc] initWithCapacity: 0]; |
60 | } | 63 | } |
61 | - [fileData setObject:data forKey:key]; | 64 | + |
62 | - [self setRequestMethod:@"POST"]; | 65 | + NSDictionary *fileInfo = [NSDictionary dictionaryWithObjectsAndKeys: |
66 | + data, @"fileDataContainerObject", contentType, @"contentType", fileName, @"fileName", nil]; | ||
67 | + [fileData setObject:fileInfo forKey:key]; | ||
68 | + [self setRequestMethod: @"POST"]; | ||
63 | } | 69 | } |
64 | 70 | ||
65 | - (void)buildPostBody | 71 | - (void)buildPostBody |
@@ -95,19 +101,21 @@ | @@ -95,19 +101,21 @@ | ||
95 | } | 101 | } |
96 | 102 | ||
97 | // Adds files to upload | 103 | // Adds files to upload |
98 | - NSData *contentTypeHeader = [[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]; | ||
99 | e = [fileData keyEnumerator]; | 104 | e = [fileData keyEnumerator]; |
100 | i=0; | 105 | i=0; |
101 | while (key = [e nextObject]) { | 106 | while (key = [e nextObject]) { |
102 | - NSString *fileName = @"file"; | 107 | + NSDictionary *fileInfo = [fileData objectForKey:key]; |
103 | - id file = [fileData objectForKey:key]; | 108 | + id file = [fileInfo objectForKey:@"fileDataContainerObject"]; |
104 | - if ([file isKindOfClass:[NSString class]]) { | 109 | + NSString *contentType = [fileInfo objectForKey:@"contentType"]; |
105 | - fileName = (NSString *)file; | 110 | + NSString *fileName = [fileInfo objectForKey:@"fileName"]; |
106 | - } | 111 | + |
107 | - [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",key,fileName] dataUsingEncoding:NSUTF8StringEncoding]]; | 112 | + NSString *contentTypeHeader = [NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType]; |
108 | - [self appendPostData:contentTypeHeader]; | 113 | + |
114 | + [self appendPostData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", key, fileName] dataUsingEncoding:NSUTF8StringEncoding]]; | ||
115 | + [self appendPostData:[contentTypeHeader dataUsingEncoding:NSUTF8StringEncoding]]; | ||
116 | + | ||
109 | if ([file isKindOfClass:[NSString class]]) { | 117 | if ([file isKindOfClass:[NSString class]]) { |
110 | - [self appendPostDataFromFile:fileName]; | 118 | + [self appendPostDataFromFile:file]; |
111 | } else { | 119 | } else { |
112 | [self appendPostData:file]; | 120 | [self appendPostData:file]; |
113 | } | 121 | } |
-
Please register or login to post a comment