AFNetworking上传图片奇怪的响应

时间:2013-06-04 21:58:03

标签: php ios afnetworking

我正在尝试使用AFNetworking将图像上传到服务器,但我收到了来自服务器的非常奇怪的回复。

有代码:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *imageToUpload = UIImageJPEGRepresentation(image, 0.8);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.theserver.net"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/public_html/imageUpload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
        [formData appendPartWithFileData: imageToUpload name:@"file" fileName:[NSString stringWithFormat:@"%@.jpg",[def valueForKey:@"cUsuario"]] mimeType:@"image/jpeg"];
    }];

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

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *response = [operation responseString];
        NSLog(@"response: [%@]",response);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if([operation.response statusCode] == 403){
            NSLog(@"Upload Failed");
            return;
        }
        NSLog(@"error: %@", [operation error]);

    }];

    [operation start];

PHP就是:

function upload(){
    $uploaddir = 'public_html/go2images/';
    $file = basename($_FILES['file']['name']);
    $uploadfile = $uploaddir . $file;

    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
       sendResponse(200, 'Upload Successful');
      return true;
    }
    sendResponse(403, 'Upload Failed');
      return false;
}

我收到的回复是HTML标签,如html,body ... 出了什么问题?

0 个答案:

没有答案