3

The reason why ask you have to do it because I confuse about params.

As I understood there one way how to do it using multi part request.

This way offers us two concepts as I understood upload from file that can be stored in Document directory or using NSData object.

Upload from the file:

So I have saved test.jpg to the document directory. Then I make NSURL instance to use it in multi part request. The code below shows how I create NSURL instance:

NSString *fileName = @"test.jpg";
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *filePath = [NSURL fileURLWithPath:folderPath];

When I print file path:

file:///Users/mac/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/F732FCF6-EAEE-4E81-A88B-76ADB75EDFD1/Documents/test.jpg

Then I set my parameters and using formData to attach my file as below:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Is this correct? or maybe I miss something because the response is not success?

The second concept - work directly with data:

[formData appendPartWithFileData:imageData name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];

but when the app invokes this line of code I got the error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: body'

The reason I suppose that the imageData defined outside the manager block is nil on this line. So I need help here as well how to pass it into the block.

Please can you correct me what's wrong in my steps maybe I miss something.

Also when I comment the line

[formData appendPartWithFileData:imageData name:@"image" fileName:@"test.jpg" mimeType:@"image/jpeg"];

or

[formData appendPartWithFileURL:filePath name:@"image" error:nil];

then everything works fine and I get success response in rerun.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • Your exception is triggered by a parameter assert inside AFNetworking. However, with only some lines of code it's pretty difficult to say why. Please paste the stack trace. Are you sure that `imageData` is not `nil`? Or the file exists and it's not empty? A breakpoint and stepping into the AFNetworking code should reveal the problem quickly. – Sulthan Mar 05 '14 at 10:04
  • @Sulthan yes the data is nil, but I pass it into my method. but in the manager block imageData is nil – Matrosov Oleksandr Mar 05 '14 at 10:24
  • can u check this question please http://stackoverflow.com/questions/33228903/uploading-image-with-afnetworking-with-null-parameter-doesnt-work/33229410?noredirect=1#comment54313681_33229410 – Nischal Hada Oct 21 '15 at 17:40

1 Answers1

6

Have you confirmed that the file is found at that location in your Documents folder? I'm also having trouble reconciling your programmatically determined file path (which is correct) with your string literal file path (which can easily be problematic). You should always programmatically determine the path, not using string literals (because when you reinstall the app, that path will change). What I think you need is something like:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSError *error;
    BOOL success = [formData appendPartWithFileURL:fileURL name:@"image" fileName:filePath mimeType:@"image/png" error:&error];
    if (!success)
        NSLog(@"appendPartWithFileURL error: %@", error);
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Note, this programmatically determines the filePath (which is a path, not a URL), and then uses fileURLWithPath to convert that to a file URL. It also confirms whether it was successful or not, logging the error if not successful.

Also note that this assumes your server is returning its response as JSON. If not, you'd have to change the responseSerializer.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • thank you for answer in your code you have if (!success) NSLog(@"appendPartWithFileURL error: %@", error); I have debuted through it and the success variable is always TRUE. So it means everything was appended fine. But the response is bad with some error 105 defined on the server – Matrosov Oleksandr Mar 05 '14 at 11:40
  • thank you it works for me, I just used wring name parameter in my case it was key with name playerImage instead of image. – Matrosov Oleksandr Mar 05 '14 at 11:44
  • can u check this question please http://stackoverflow.com/questions/33228903/uploading-image-with-afnetworking-with-null-parameter-doesnt-work/33229410?noredirect=1#comment54313681_33229410 – Nischal Hada Oct 21 '15 at 17:40