简体   繁体   中英

AFNetworking 2.0 POST + PHP block failure called although upload is successful

Description of my problem is actually in question header. I'm new with web technologies, so this behaviour is for me a little striking. Why failure block is called although image upload has been completed with success? Moreover I searched an answer for my question on SoF and applied some solutions, but this error still is appearing. Please help if possible. Thank you in advance.

OBJECTIVE - C:

- (void)upload {

    NSString *fileName = [NSString stringWithFormat:@"%ld%c%c.jpg", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a'];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"jpg"];
    NSData *data = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:imagePath]);

    NSString *URLString = @"http://myServer/myAppFolder";

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager manager] initWithBaseURL:[NSURL URLWithString:URLString]];

    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    [manager setResponseSerializer:responseSerializer];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

    AFHTTPRequestOperation *operation = [manager POST:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:data name:@"uploadedfile" fileName:fileName mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure / %@, /  %@", error, operation.responseString); //error comes from here
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"uploading...");
    }];
}

upload.php:

<?php
$filename="uploaded";
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded";
} else {
    echo "There was an error uploading the file, please try again!";
}
?>;

I'm getting this error:

Failure / Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x8a633e0 {NSDebugDescription=Invalid value around character 0.}, /  The file 1389046572lb.jpg has been uploaded;

It seems that the problem is in the fact that you are using AFJSONResponseSerializer to serialize the response of your PHP upload script.

And your PHP is not returning a JSON - thus serializer throws an error.

You could either modify PHP script so that it returns a JSON formatted result. Or you can use different type of serializer.

According to this forum solution would be to use [AFResponseSerializer serializer] - i'm not sure if it even exists. Try basic AFHTTPResponseSerializer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM