Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS HTTP Image Upload to Web Server
// HTTP method to upload file to web server
- (void)uploadToServerUsingImage:(NSData *)imageData andFileName:(NSString *)filename {
// set this to your server's address
NSString *urlString = @"http://fineuploader.com/demos.html#amazon-demo";
// set the content type, in this case it needs to be: "Content-Type: image/jpg"
// Extract 'jpg' or 'png' from the last three characters of 'filename'
if (([filename length] -3 ) > 0) {
NSString *contentType = [NSString stringWithFormat:@"Content-Type: image/%@", [filename substringFromIndex:[filename length] - 3]];
}
// allocate and initialize the mutable URLRequest, set URL and method.
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
// define the boundary and newline values
NSString *boundary = @"uwhQ9Ho7y873Ha";
NSString *kNewLine = @"\r\n";
// Set the URLRequest value property for the HTTP Header
// Set Content-Type as a multi-part form with boundary identifier
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// prepare a mutable data object used to build message body
NSMutableData *body = [NSMutableData data]
// set the first boundary
[body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
// Set the form type and format
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"%@", @"uploaded_file", filename, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: image/jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
// Now append the image itself. For some servers, two carriage-return line-feeds are necessary before the image
[body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
// Add the terminating boundary marker & append a newline
[body appendData:[[NSString stringWithFormat:@"--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
// Setting the body of the post to the request.
[request setHTTPBody:body];
// TODO: Next three lines are only used for testing using synchronous conn.
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"==> sendSyncReq returnString: %@", returnString);
// You will probably want to replace above 3 lines with asynchronous connection
// NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment