Uyghur Ilan
Uyghur Ilan

Reputation: 113

AFNetworking file upload

I'm trying to upload file with AFNetworking and it is working with iOS Simulator but not working Real devices, i tried 2 different way but same.

Can some one help me to solve this issue please Thank you very much.

this is first code

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *dir = [paths objectAtIndex:0];
NSString *mp3File = [dir stringByAppendingPathComponent:@"Mp3File.mp3"];


NSData* myData = [mp3File dataUsingEncoding:NSUTF8StringEncoding];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"nid": nid, @"uid": [NSString stringWithFormat:@"%@", [appSet objectForKey:@"userid"]]};
AFHTTPRequestOperation *op = [manager POST:@"http://192.168.1.103/sample_Files/mp3.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    [formData appendPartWithFileData:myData name:@"userfile" fileName:[NSString stringWithFormat:@"%@.mp3", dateString] mimeType:@"audio/mpeg"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {

     NSLog(@"YESSS UPLOADED");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
}];

[op start];

and this is other one

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"nid": nid, @"uid": [NSString stringWithFormat:@"%@", [appSet objectForKey:@"userid"]]};
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *filePath = [documentsPath stringByAppendingPathComponent:@"Mp3File.mp3"];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];

    AFHTTPRequestOperation *op =  [manager POST:@"http://192.168.1.103/sample_Files/mp3.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSError *error;
        BOOL success = [formData appendPartWithFileURL:fileURL name:@"userfile" fileName:[NSString stringWithFormat:@"%@.mp3", dateString] mimeType:@"audio/mpeg" error:&error];
        if (!success)
            NSLog(@"appendPartWithFileURL error: %@", error);

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

    }];
    [op start];

Upvotes: 0

Views: 682

Answers (1)

Larry B
Larry B

Reputation: 193

From what I've seen it looks correct.

Have you set a breakpoint on the FAILURE to see what the NSERROR response is?

Here's a sample URL that posted a very similar solution.

Uploading image with AFNetworking 2.0

Upvotes: 1

Related Questions