0

Can anyone tell me how to upload audio files with the help of AFNetworking on a server in iOS? Is this ok?

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

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

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

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

    // etc.
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Response: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    }];
Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
Ethan
  • 29
  • 4

1 Answers1

0

You can use AFURLSessionManager to make an upload task, here's the way:

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);
    } else {
        NSLog(@"Success: %@ %@", response, responseObject);
    }
}];
[uploadTask resume];
Meseery
  • 1,845
  • 2
  • 22
  • 19