0

i am new at IOS and am working for uploading a video file on server using NSURLConnection. When i upload video on server it returns status code as 200 but the dictionary objects show as:

    Dictionary {
OK = 0;
info = "File not found.";
}

I am completely fed up with all this for not getting whether its server problem or problem at my side.my code for this is

    NSArray *file=[[appDelegate.array_Allideos objectAtIndex:buttonTag] componentsSeparatedByString:@"."];
    NSString *date=[file objectAtIndex:0];
    NSString *extension=[file objectAtIndex:1];
    NSString *video_title=[NSString stringWithFormat:@"Video%d.mov",buttonTag+1];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDirectory,[appDelegate.array_Allideos objectAtIndex:buttonTag]];

    NSData *videoData=[NSData dataWithContentsOfFile:filePath];
    NSString *encodedFile=[videoData base64Encoding];

    NSMutableDictionary *dictionary=[NSMutableDictionary new];
    [dictionary setObject:@"xyz@gmail.com" forKey:@"UserID"];
    [dictionary setObject:video_title forKey:@"video_title"];
    [dictionary setObject:encodedFile forKey:@"video_stream"];
    [dictionary setObject:date forKey:@"recorded_date"];

    NSURL *url=[NSURL URLWithString:@"my url"];

    NSError *error;
    NSDictionary *jsonDict=[NSDictionary dictionaryWithDictionary:dictionary];

    NSData *jsondata = [NSJSONSerialization dataWithJSONObject:jsonDict                                                   options:NSJSONWritingPrettyPrinted error:&error];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSLog(@"Length: %u",[jsondata length]);
    //[request setValue:@"text/html,application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"multipart/form-data; boundary=AaB03x" /*@"image/png,charset=utf-8,application/x-www-form-urlencoded" */forHTTPHeaderField:@"Content-Type"];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"%d", [jsondata length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:jsondata];

    NSOperationQueue *queue = [NSOperationQueue currentQueue];

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *err)
     {
         if ([data length] > 0 && err == nil){
             NSError *parseError = nil;
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
             NSLog(@"Server Response (we want to see a 200 return code) %@",response);
             NSLog(@"\nDictionary %@",dict);
             //if response is generated sucessfully
         }
         else if ([data length] == 0 && err == nil){
             NSLog(@"No data returned");
             //no data, but tried
         }
         else if (err != nil)
         {
             NSLog(@"There was a error: %@",err);
             //couldn't execute request
         }
     }];

}
  • are you talking about HTTP status code for code 200? – Wingzero May 26 '15 at 09:12
  • yes status code in response – Himanshi Modani May 26 '15 at 09:13
  • a HTTP code 200 is the HTTP protocol status code, it just means your server get your request and it handled it and return you 200 as success. You might need to check your server side code and logs to find some clues – Wingzero May 26 '15 at 09:15
  • yes i understand this... but i have been told that server is not getting any file from my side to upload and status code is 200 just because of the fact that your application has hit the api... – Himanshi Modani May 26 '15 at 09:17
  • http://stackoverflow.com/questions/7266464/nsurlconnection-to-upload-file-asynchonrously and http://stackoverflow.com/questions/15834690/send-a-file-using-nsurlconnection may help you. – Wingzero May 26 '15 at 09:19
  • I am no expert on HTTP protocol. I suggest you use a proxy tool to hijack your HTTP request and check if your are giving the right paramter and data. If you do, ask server guys to check if the request containing the video data you upload – Wingzero May 26 '15 at 09:27

0 Answers0