-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHTTPFileUpload.m
More file actions
159 lines (134 loc) · 6.31 KB
/
Copy pathHTTPFileUpload.m
File metadata and controls
159 lines (134 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// HTTPFileUpload.m
//
// Version: 1.01
//
// Created by tochi on 11/06/10.
// Copyright 2011 aguuu Inc. All rights reserved.
//
// License: MIT License
//
#import "HTTPFileUpload.h"
#define KEY_POST_NAME @"postName"
#define KEY_POST_STRING @"postString"
#define KEY_POST_IMAGE @"postImage"
#define KEY_POST_IMAGE_FILE_NAME @"postImageFileName"
#define BOUNDARY @"----iOSAppsFormBoundaryByHTTPFileUpload"
@implementation HTTPFileUpload
@synthesize delegate=delegate_;
- (id)init {
self = [super init];
if (self) {
postStrings_ = [[NSMutableArray alloc] initWithCapacity:0];
postImages_ = [[NSMutableArray alloc] initWithCapacity:0];
}
return self;
}
- (void)dealloc
{
delegate_ = nil, [delegate_ release];
[postStrings_ release], postStrings_ = nil;
[postImages_ release], postImages_ = nil;
[resultData_ release], resultData_ = nil;
[super dealloc];
}
- (void)setPostString:(NSString *)stringValue
withPostName:(NSString *)postName
{
NSDictionary *stringDictionary;
stringDictionary = [NSDictionary dictionaryWithObjectsAndKeys:stringValue, KEY_POST_STRING,
postName, KEY_POST_NAME, nil];
[postStrings_ addObject:stringDictionary];
}
- (void)setPostImage:(UIImage *)image
withPostName:(NSString *)postName
fileName:(NSString *)fileName
{
NSDictionary *imageDictionary;
imageDictionary = [NSDictionary dictionaryWithObjectsAndKeys:image, KEY_POST_IMAGE,
postName, KEY_POST_NAME,
fileName, KEY_POST_IMAGE_FILE_NAME, nil];
[postImages_ addObject:imageDictionary];
}
- (void)postWithUri:(NSString *)uri
{
NSMutableData *postData = [[[NSMutableData alloc] init] autorelease];
// Create string data.
for (NSDictionary *stringDictionary in postStrings_) {
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n",
[stringDictionary objectForKey:KEY_POST_NAME]] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"%@\r\n",
[stringDictionary objectForKey:KEY_POST_STRING]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// Create image data.
NSRegularExpression *regExp;
NSTextCheckingResult *match;
NSError *error = nil;
regExp = [NSRegularExpression regularExpressionWithPattern:@"[.](?:jpg|jpeg)$"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSData *imageData;
NSString *contentType;
for (NSDictionary *imageDictionary in postImages_) {
match = [regExp firstMatchInString:[imageDictionary objectForKey:KEY_POST_IMAGE_FILE_NAME]
options:0
range:NSMakeRange(0, [[imageDictionary objectForKey:KEY_POST_IMAGE_FILE_NAME] length])];
if (match != nil) {
imageData = UIImageJPEGRepresentation([imageDictionary objectForKey:KEY_POST_IMAGE], 1.0f);
contentType = @"image/jpeg";
} else {
imageData = UIImagePNGRepresentation([imageDictionary objectForKey:KEY_POST_IMAGE]);
contentType = @"image/png";
}
[postData appendData:[[NSString stringWithFormat:@"--%@\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",
[imageDictionary objectForKey:KEY_POST_NAME],
[imageDictionary objectForKey:KEY_POST_IMAGE_FILE_NAME]] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[postData appendData:[[NSString stringWithFormat:@"--%@--\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
// Post data.
NSMutableURLRequest *request;
request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:uri]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30] autorelease];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
resultData_ = [[NSMutableData alloc] initWithCapacity:0];
[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
}
#pragma mark - NSURLConnection delegate.
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
if ([delegate_ respondsToSelector:@selector(httpFileUpload:didFailWithError:)]) {
[delegate_ httpFileUpload:connection didFailWithError:error];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[resultData_ release], resultData_ = nil;
[connection cancel];
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[resultData_ appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *result = [[[NSString alloc] initWithData:resultData_ encoding:NSUTF8StringEncoding] autorelease];
if ([delegate_ respondsToSelector:@selector(httpFileUploadDidFinishLoading:result:)]) {
[delegate_ httpFileUploadDidFinishLoading:connection result:result];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[resultData_ release], resultData_ = nil;
[connection cancel];
}
@end