3

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Dinesh Kaushik
  • 2,917
  • 2
  • 23
  • 36

2 Answers2

4
 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

xphobia
  • 126
  • 6
  • I will try it and will let you know. – Dinesh Kaushik Sep 15 '14 at 02:21
  • I get the following error using above code Failure Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (403)" UserInfo=0xaea3bf0 {com.alamofire.serialization.response.error.response= 403 Forbidden

    Forbidden

    You don't have permission to access /upload_test/ on this server.

    – Dinesh Kaushik Sep 18 '14 at 07:27
  • what parameters do i need to send ?? any other server configuration required ?? – Dinesh Kaushik Sep 18 '14 at 07:29
  • i think your api needs some token or authentication to access – xphobia Sep 18 '14 at 08:48
  • http://stackoverflow.com/questions/7991927/error-code-1011-when-i-use-afnetworking – xphobia Sep 18 '14 at 08:49
  • I am looking at the link provided but did you used any php or any other script to upload the file ?? i am using your code like this Your server base url: http://server11.com url to upload image : /upload_test – Dinesh Kaushik Sep 18 '14 at 10:04
  • yeah i used php script but it was done by web personal, i used a token to access the api – xphobia Sep 18 '14 at 10:29
  • Where did you gave the refrence of the php script in native objective c code. Can u share the php script if possible ? – Dinesh Kaushik Sep 18 '14 at 10:34
  • well i don't have the script but i think the problem is from web side as you don't have access on upload_test folder in the server – xphobia Sep 18 '14 at 10:43
  • Where did you gave the reference of the php script in native objective c code for file upload ?? – Dinesh Kaushik Sep 18 '14 at 10:44
  • Thanks a lot , I am able to upload image successfully after using the self created php script but not able to upload video file of size (3MB). – Dinesh Kaushik Sep 18 '14 at 11:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61483/discussion-between-dinesh-kaushik-and-xphobia). – Dinesh Kaushik Sep 18 '14 at 11:07
  • name:@"file" fileName:@"image.png" mimeType:@"image/png" for this parameters does server needs to create these or it is by default property – Nischal Hada Oct 21 '15 at 07:33
  • can u take a look in this question http://stackoverflow.com/questions/33228903/uploading-image-with-afnetworking-with-null-parameter-doesnt-work – Nischal Hada Oct 21 '15 at 07:47
0

This error is from the API where we send data with HTTP code, So please in the API code that if they are sending data then there always the http code should be 200

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84