Process
- Make a NSMutableURLRequest
- Set the URL for NSMutableURLRequest
- Set HTTP method for NSMutableURLRequest
- Set header fields for NSMutableURLRequest
- Set HTTP body for NSMutableURLRequest. This will include the image file and any other parameters (sample code below)
- Initialise a previously declared NSMutableData object. This is used to store the response from the server (sample code below)
- Initialise a previously declared NSURLConnection object and set its delegate. In the sample code I have made the same class as the delegate and implemented the delegate methods.
Sample code
- (void)uploadImage
NSString *urlStr = @"your link to the server";
NSMutableURLRequest *urlRequest = [[[NSMutableURLRequest alloc] init] autorelease];
[urlRequest setURL:[NSURL URLWithString:urlStr]];
[urlRequest setHTTPMethod:@"POST"];
NSString *myboundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",myboundary];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postData = [NSMutableData data];
NSString *filePath = @"path to file you want to upload";
NSData *zipData = [NSData dataWithContentsOfFile:filePath];
if(zipData != nil){
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", @"file.zip"]dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:zipData]];
}
NSString *params =@"Any other parameters you want to send with the file";
NSLog(@"%@",params);
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"params"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"%@", params] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", myboundary] dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPBody:postData];
[urlRequest setValue:[SettingsDataStore getSharedInstance].cooike forHTTPHeaderField:@"Cookie"];
responsedata = [[NSMutableData alloc] initWithData:nil];
conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}
//NSURLConnection Delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responsedata appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error occured %@", [error localizedDescription]);
[responseData release];
[conn release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//Do something with responseData and release it
[responseData release];
[conn release];
}