user3214941
user3214941

Reputation: 397

Can not upload video with multi part POST in AFNETWORKING on iOS

I tried below code to upload video with Multi part form POST in AFnetworking but when uploading, video sent about 80% is broken. This is my code:

    -(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];


}

My code has any problem? Please give me some advice. thanks in advance

Upvotes: 0

Views: 2153

Answers (1)

Luca Iaco
Luca Iaco

Reputation: 3457

I suggest you to use appendPartWithFileURL instead of appendPartWithFormData for files, to avoid memory problems (imagine big data like video or compressed data files).

I use something like this:

// 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];
}];

Upvotes: 2

Related Questions