dongqian1925 2013-12-21 21:38
浏览 80

AFNetworking 2.0图片上传和JSON返回错误

My AFNetworking code does not seem to be uploading any image and text to the server, but when I attempt to upload the image to the PHP file, the Json return an error listed below, or Error Code 3840. Any ideas would be appreciated and let me know if you need more information.

-(void)upLoadImage
{ 
   AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"https://***.php"]];

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.5);
NSDictionary *parameters = @{@"message": self.pictureMessage.text};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {


    //do not put image inside parameters dictionary as I did, but append it!
    [formData appendPartWithFileData:imageData name:@"userfl" fileName:@"upload.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];


op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

}

PHP:

    <?php

header('Content-type: application/json');

if(!empty($_POST)) 
{

    $message = $_POST['message'];

    $directory = $_SERVER['DOCUMENT_ROOT'] . '/appphotos';

    $file = basename($_FILES['userfl']['upload.jpg']);

    $uploadfile = $directory . $file;

    echo 'message= '.$message;

    var_dump($_FILES);

    $randomPhotoID = md5(rand() * time());

    echo json_encode($file);

if (move_uploaded_file($_FILES['userfl']['tmp_name'], $uploadfile)) 
    {
    $response["success"];
    echo json_encode($response);
    }
    else
    {
    $response["failure"];
    echo json_encode($response);

}

}
else
{
    $response['Empty post data'];
    echo json_encode($response);
}

?>

Error:

2013-12-23 01:06:45.720 Animal[13757:907] Success: message= array(1) {
  ["userfl"]=>
  array(5) {
    ["name"]=>
    string(10) "upload.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(14) "/tmp/phpeRVNwb"
    ["error"]=>
    int(0)
    ["size"]=>
    int(9613)
  }
}
""null ***** <6d657373 6167653d 20617272 61792831 29207b0a 20205b22 75736572 666c225d 3d3e0a20 20617272 61792835 29207b0a 20202020 5b226e61 6d65225d 3d3e0a20 20202073 7472696e 67283130 29202275 706c6f61 642e6a70 67220a20 2020205b 22747970 65225d3d 3e0a2020 20207374 72696e67 28313029 2022696d 6167652f 6a706567 220a2020 20205b22 746d705f 6e616d65 225d3d3e 0a202020 20737472 696e6728 31342920 222f746d 702f7068 70655256 4e776222 0a202020 205b2265 72726f72 225d3d3e 0a202020 20696e74 2830290a 20202020 5b227369 7a65225d 3d3e0a20 20202069 6e742839 36313329 0a20207d 0a7d0a22 226e756c 6c>
  • 写回答

1条回答 默认 最新

  • doulu8537 2014-08-20 18:09
    关注

    try this?

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据