0

I need to implement a subclass of NSOperation which does a file upload to a HTTP server and with an option that the user can cancel the file upload during operation.

Here is the code at the moment:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:filename];

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

  [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  [request setHTTPShouldHandleCookies:NO];
  [request setTimeoutInterval:20];
  [request setHTTPMethod:@"POST"];

  NSString *boundary = @"------WebKitFormBoundary4QuqLuM1cE5lMwCy";

  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

  NSMutableData *body = [NSMutableData data];
  NSString *FileParamConstant = @"uploadFile";
  NSData *imageData = [[NSData alloc] initWithContentsOfFile:imagePath];

  NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:11];
  [parameters setValue:@"Value" forKey:@"Server_required_param"];

  NSString *urlString = @"http://www.omeuendereco/uploadFile.php";   

  for (NSString *param in parameters) {

      [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", FileParamConstant, filename] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:imageData];
      [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  }

  [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

  [request setHTTPBody:body];
  [request setURL:[NSURL URLWithString:urlString]];

  NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                                delegate:up
                                                        startImmediately:YES]; 

  [connection start];

the problem is that i have to implement a NSOperationqueue in this code and i dont know how. Then with the subclass of NSOperation i will be able to cancel the upload operation.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
berlos
  • 1

1 Answers1

-1

What the hell is your problem ? If you don't know how to make a subclass - learn Objective-C.

If I don't understand you (and there's some sophiscated problem, but I don't think so), you've got two solutions.

  1. CREATE THAT SUBCLASS and then make an instance of NSOperationQueue, add an instance of that subclass to the queue.

  2. Use AFNetworking or MKNetworkKit(MKNetworkKit uses NSOperation and NSOperationQueue)

wczekalski
  • 720
  • 4
  • 21
  • It was to make an impact on him. No no, seriously the question makes no sense and I tried to express it . Sorry if you were offended – wczekalski Aug 16 '12 at 18:07
  • 4
    I am not a prude and I don't personally mind swearing. "What is your problem?" is perfectly justified. "What the hell is your problem?" could just as well read like a personal attack which, regardless of whether it is justified, is bad style, unhelpful and probably also forbidden by the rules. If your problem with the question is that it is ambiguous, try to avoid that problem yourself. – Jesper Aug 20 '12 at 07:36