Ios Iphone:如何将NSData视频文件分割成块,并将块逐个上传到服务器?

Ios Iphone:如何将NSData视频文件分割成块,并将块逐个上传到服务器?,ios,file-upload,asihttprequest,nsdata,chunks,Ios,File Upload,Asihttprequest,Nsdata,Chunks,我一直在尝试将视频文件分割成块,并将块逐个发送到服务器。但我不确定我写的逻辑或代码是否正确!如果你能帮我做这个 -(void)multipart:(NSData *)file ; { NSDate *currentTime = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh-mm"]; NSString *result

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

-(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);
}
-(void)多部分:(NSData*)文件;
{
NSDate*currentTime=[NSDate日期];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[日期格式化程序setDateFormat:@“hh-mm”];
NSString*resultString=[dateFormatter stringFromDate:currentTime];
myDid=[[NSString alloc]initWithString:[[UIDevice currentDevice]uniqueIdentifier]];
upload_Id=[NSString stringWithFormat:@“%@%@”,myDid,resultString];
NSLog(@“%@”,上载Id);
NSU整数长度=[文件长度];
NSU整数chunkSize=100*1024;
整数偏移=0;
做{
NSUInteger thisChunkSize=长度-偏移>chunkSize?chunkSize:长度-偏移;
NSData*chunk=[NSData datawithbytesnopy:(char*)[文件字节]+偏移量长度:thishchunksize freehendone:YES];
//NSLog(@“%@”,块);
//NSLog(@“%@”,thishchunksize);
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];
偏移量+=此块大小;
NSLog(@“%d”,偏移量);
//[chunk writeToFile:[self.recordedChunkFile path]原子:是];
//NSLog(@“%@,[self.recordedChunkFile path]);
//NSString*path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@“%@:/%@/%/%/%@”,[[NSSERDEFAULTS standardUserDefaults]objectForKey:DEFAULTS\u SET\u HTTP\u SWITCH],DB\u SAVED\u SERVER,API\u MULTIPART\u UPLOAD];
__block ASIFormDataRequest*request=[AsiformDataRequestRequestWithURL:url];
//[请求集文件:[self.recordedChunkFile路径]forKey:@“data1”];
//上载NSData实例
[请求setData:chunk with filename:upload_Id and contenttype:@“video/mov”forKey:@“data1”];
[请求setPostValue:mainString forKey:@“jsonData”];
}而(偏移量<长度);
}

谢谢

在我看来,拆分数据是可以的。但是你有没有处理过一些区块上传失败的案例。请考虑这些问题,然后相应地修改您的代码。我不确定如何将区块上传到我的url:(有人可以帮助我解决代码问题,以便如何将区块上传到我的url。