0

我正在尝试使用 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... 出了什么问题?

4

0 回答 0