I currently upload images in the background to a server like this (the names get changed at the server to something better).
buildURL = [buildURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 1.0); //change Image to NSData
if (imageData != nil)
{
NSString *filenames = [NSString stringWithFormat:@"imagename"]; //set name here
NSLog(@"%@", filenames);
NSString *urlString = buildURL;
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Returned From Image Upload : %@",returnString);
}];
}
This works fine, I can see the images finish in the background no problem. The thing is I want to change this to use NSURLSession and the delegate didSendBodyData so that I can monitor the upload.
I have found a load of information about the download but none really about upload. I have tried to do this with the request BUT the completion block NEVER happens.. I have also tried using uploadTaskWithStreamedRequest but I can not get the delegate to happen...
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
NSURLSessionDataTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSLog(@"Image Uploaded ------------");
}];
[uploadTask resume];
I also found this StackOverflow 19985353 which seems to highlight some problems but I could really do with some example code. The image is picked from imagePicker if that helps.
I also tried both the answers here StackOverflow 19099448 but neither of them worked, again the block never executed. Is there something fundamental I am doing wrong with NSURLSession, a framework maybe! I also note I have no idea about the HTTP body which I have tried to play with but no luck.