Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking file upload fails

Hi I am using following code for uploading image on server

AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];

    NSMutableURLRequest *request =
    [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://test.com/upload_test/" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:dataToPost
                                    name:@"attachment"
                                fileName:@"myimage.png"
                                mimeType:@"image/png"];
    } error:nil];


    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    AFHTTPRequestOperation *operation =
    [manager HTTPRequestOperationWithRequest:request
                                     success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                         NSLog(@"Success %@", responseObject);
                                     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                         NSLog(@"Failure %@", error.description);
                                     }];


    [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                        long long totalBytesWritten,
                                        long long totalBytesExpectedToWrite) {
        NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
    }];


    [operation start];

and I am getting the following error

Failure Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0xc0428a0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0xaa389e0> { URL: http://test.com/upload_test/ } { status code: 404, headers {
    Connection = "Keep-Alive";
    "Content-Length" = 210;
    "Content-Type" = "text/html; charset=iso-8859-1";
    Date = "Fri, 05 Sep 2014 07:32:49 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    Server = "Apache/2.2.15 (Win32) PHP/5.3.5";
} }, NSErrorFailingURLKey=http://test.com/upload_test/, NSLocalizedDescription=Request failed: not found (404)

I have tried on one another server on which control reaches to success block But no file is uploaded to the server.

What are the server configuration or permission required for file upload ?

I am working on iOS 7.

like image 383
Dinesh Kaushik Avatar asked Feb 14 '26 05:02

Dinesh Kaushik


1 Answers

 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"Your server base url"]];
 NSData *imageData = UIImagePNGRepresentation(image);
[manager POST:@"url to upload image" parameters:@"parameter required by the server" constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"file" fileName:@"image.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failure %@, %@", error, operation.responseString);
}];

Its Working for me. I hope it helps

like image 169
xphobia Avatar answered Feb 15 '26 19:02

xphobia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!