4

I have been trying to split the video file into chunks and send the chunks to the server one by one. But i am not sure if the logic or the code i have written is right! If you could please help me with this.

-(void)multipart:(NSData *)file ;
{
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];

myDid = [[NSString alloc]initWithString:[[UIDevice currentDevice]uniqueIdentifier]];

upload_Id = [NSString stringWithFormat:@"%@%@", myDid, resultString];
NSLog(@"%@", upload_Id);


NSUInteger length = [file length];
NSUInteger chunkSize = 100 * 1024;
NSUInteger offset = 0;
do {
    NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
    NSData *chunk = [NSData dataWithBytesNoCopy:(char *)[file bytes] + offset length:thisChunkSize freeWhenDone:YES];       

    //NSLog(@"%@", chunk);
    //NSLog(@"%@", thisChunkSize);
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"mov", @"uploadExtn", upload_Id, @"uploadId",offset , @"uploadedByte",  nil];
    NSDictionary *mainDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@"createContent",@"type",dictionary,@"data", nil];

    NSString *mainString = [mainDictionary JSONRepresentation];

    offset += thisChunkSize;
    NSLog(@"%d", offset);
    //[chunk writeToFile:[self.recordedChunkFile path] atomically:YES];
    //NSLog(@"%@", [self.recordedChunkFile path]);
    //NSString* path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask,YES) lastObject];

      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@/%@",[[NSUserDefaults standardUserDefaults]                           objectForKey:DEFAULTS_SET_HTTP_SWITCH],DB_SAVED_SERVER,API_MULTIPART_UPLOAD]];

    __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    //[request setFile:[self.recordedChunkFile path] forKey:@"data1"];
    // Upload an NSData instance
    [request setData:chunk withFileName:upload_Id andContentType:@"video/mov" forKey:@"data1"];
    [request setPostValue:mainString forKey:@"jsonData"];




} while (offset < length);
}

Thank you

Chiran
  • 183
  • 1
  • 5
  • 14
  • In my opinion, as far as splitting the data is fine. But have you handled the cases when some chunk failed to upload. Please take into consideration such issues and then modify your code accordingly. – Sagrian Mar 06 '13 at 12:39
  • I am not sure about how to upload the chunks to my url :( Could someone help me out with the code so as how to upload chunks to my url. – Chiran Mar 06 '13 at 12:57
  • http://stackoverflow.com/questions/2899020/split-nsdata-objects-into-other-nsdata-objects-with-a-given-size http://stackoverflow.com/questions/1065628/uploading-video-with-iphone – chirag Apr 04 '13 at 08:44

0 Answers0