iPhone upload multipart file using AFNetworking

Looking at your HTML, the name of your <input type=file> is files, and thus, you would use @"files" as the name parameter to the appendPartWithFileData method. For example, with AFNetworking 3.x:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"files"
                            fileName:photoName mimeType:@"image/jpeg"];

    [formData appendPartWithFormData:[key1 dataUsingEncoding:NSUTF8StringEncoding]
                                name:@"key1"];

    [formData appendPartWithFormData:[key2 dataUsingEncoding:NSUTF8StringEncoding]
                                name:@"key2"];

    // etc.
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
}];

(For AFNetworking 1.x and 2.x syntax, see the revision history of this answer.)

Leave a Comment