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.
See Question&Answers more detail:
os