4

I'm using AFNetworking to send a multipart form to a web-server, and i'm having some trouble with my AFHTTPRequestOperation. It's success and failure blocks are never called, after i start it.

Here is my code (a resume of it)

    NSMutableURLRequest *request = [[ServerAPI sharedClient] multipartFormRequestWithMethod:@"POST" path:postUrl parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
        [formData appendPartWithFileData:picture.picture_data name:@"InputFile" fileName:picture.name mimeType:@"image/jpg"];
    }];

    AFHTTPRequestOperation *operation = [[ServerAPI sharedClient] HTTPRequestOperationWithRequest: request success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
    } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
    }];         
    [[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];

I can see the logs of the progress, but success and failure blocks are never called.

picture.picture_data is a NSData initialized with a UIImageJPEGRepresentation(image, 0.7) ServerAPI is a subclass of AFHTTPClient, and sharedCliend is a singleton method.

Which are the reasons for AFNetworking don't call my blocks, not even with an proper error message?

Thank you all!

Edit

I do a get request with the same URL just before this one, and it works as usual. The URL i'm using is: part/_layouts/UploadEx.aspx?List=%7BD432BF97-7175-40C1-8E0D-27D8661CBC90%7D&RootFolder=%2Fpwa%2Fpart%2FLibrary&Source=http%3A%2F%2Fwww%2Emysite%2Ecom%2Fpwa%2Fpart%2FLibrary%2FForms%2FAllItems%2Easpx&IsDlg=1

Crystian Leão
  • 695
  • 1
  • 8
  • 18

1 Answers1

0

In your code, check your postUrl . The BaseURL+postURL must be valid. Try upload image using normal web browser using URL BaseURL+postURL.

Edit

method HTTPRequestOperationWithRequest:success:failure: does not work for file uploading, but works for json/html fetching. Try use

AFHTTPRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];

[operation setUploadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"%f", (totalBytesRead / (float) totalBytesExpectedToRead));
    }];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure");
    }];

[[ServerAPI sharedClient] enqueueHTTPRequestOperation:operation];
Alphapico
  • 2,893
  • 2
  • 30
  • 29