4

我一直在尝试将视频文件拆分成块并将这些块一个一个地发送到服务器。但我不确定我写的逻辑或代码是否正确!如果你能帮我解决这个问题。

-(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);
}

谢谢

4

0 回答 0