Не удается загрузить видео с многокомпонентным POST в AFNETWORKING на iOS

Я попытался в приведенном ниже коде загрузить видео с Multi part form POST в AFnetworking, но при загрузке видео, отправленное примерно на 80%, не работает. Это мой код:

    -(void) uploadVideoAPI: (NSString*) emailStr andSumOfFiles: (NSString*) sumSizeFile  andVideoNams:(NSMutableArray*) videoNameArr andUpFile :(NSMutableArray *) videoDataArray
{
    NSURL *url = [NSURL URLWithString:@"http://myserver.com];

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: url] ;

    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFormData:[emailStr dataUsingEncoding:NSUTF8StringEncoding]
                                    name:@"emailStr"]; //parametters1

        [formData appendPartWithFormData:[sumSizeFile dataUsingEncoding:NSUTF8StringEncoding] name:@"sumSizeFile"];//parametters 2

        for(int i=0;i<[videoDataArray count];i++)
        {
            NSString * videoName = [videoNameArr objectAtIndex:i];
            NSData *videoData = [videoDataArray objectAtIndex:i];
            [formData appendPartWithFileData:videoData
                                        name:@"videos"
                                    fileName:videoName mimeType:@"video/quicktime"];
        }

    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

    }];
    [httpClient enqueueHTTPRequestOperation:operation];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Upload Complete");
    }
                                     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"error: %@", operation.responseString);
                                         NSLog(@"%@",error);
                                     }];
    [operation start];


}

В моем коде есть проблемы? Пожалуйста, дайте мне совет. заранее спасибо


person user3214941    schedule 21.01.2014    source источник
comment
Попробуйте мой ответ stackoverflow.com/questions/40084703/   -  person Jitendra Modi    schedule 17.10.2016


Ответы (1)


Я предлагаю вам использовать appendPartWithFileURL вместо appendPartWithFormData для файлов, чтобы избежать проблем с памятью (представьте себе большие данные, такие как видео или сжатые файлы данных).

Я использую что-то вроде этого:

// Create request
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) {
    // Important!! : file path MUST BE real file path (so -> "file://localhost/.../../file.txt") so i use [NSURL fileURLWithPath:]
    NSError* err;
    [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePathToUpload] name:[fileInfo objectForKey:@"fileName"] error:&err];
}];
person Luca Iaco    schedule 21.01.2014