使用AF在ios中工作的多部分格式的图像上传

时间:2014-10-24 12:47:30

标签: php ios objective-c cocoa afnetworking

  1. AFHTTPSessionManager用于从ios app发布文件
  2. 服务器端使用php
  3. 实现
  4. 如果文件是从Web服务器发送的,则服务器接受该文件
  5. 我的iOS代码

    UIImage *image = [UIImage imageNamed:@"Fb-button"];
    
    NSData *imageData = UIImagePNGRepresentation(image);
    
    AFHTTPSessionManager *sessionManager  = [[AFHTTPSessionManager alloc]initWithBaseURL:@"baseurl"];
    
    AFJSONResponseSerializer *jsonresponseSerilaizer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    
    jsonresponseSerilaizer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",@"text/html", nil];
    
    sessionManager.responseSerializer = jsonresponseSerilaizer ;
    
    [sessionManager POST:@"pathurl" parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    
        [formData appendPartWithFileData: imageData name:@"userfile" fileName:@"userfile.png" mimeType:@"png"];
    
    } success:^(NSURLSessionDataTask *task, id responseObject) {
    
        NSLog(@"success");
    
        NSLog(@"response : %@",responseObject);
    
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    
        NSLog(@"error");
    
        NSLog(@"error : %@",error);
    
    }];
    

    我在xcode中遇到的错误是可可错误3840

    error : Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x79865f70 {NSDebugDescription=Invalid value around character 0., NSUnderlyingError=0x798665c0 "Request failed: bad request (400)"}

    PHP代码如下所示

    $user_image              =   $_FILES['user_image']['name'];
    $config['upload_path']   = './images/User_files/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['file_name']     = $user_image;
    $config['max_size']      = '100';
    $config['max_width']     = '1024';
    $config['max_height']    = '768';
    
    $this->load->library('upload', $config);
    
    if ( ! $this->upload->do_upload())
    {
        $data["error"] = array('error' => $this->upload->display_errors());
    
        echo json_encode($data);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
    
        echo '{"success": "true"}';
    }
    

    我还尝试了以下php代码:

    if($_FILES['user_image']['size'] > 0) 
    {
        $user_image = substr(number_format(time() * rand(), 0, '', ''), 0, 8);
    
        if (preg_match('/^image\/p?jpeg$/i', $_FILES['user_image']['type']) or 
            preg_match('/^image\/gif$/i', $_FILES['user_image']['type']) or 
            preg_match('/^image\/jpg$/i', $_FILES['user_image']['type']) or 
            preg_match('/^image\/(x-)?png$/i', $_FILES['user_image']['type'])) 
        { 
            if (preg_match('/^image\/p?jpeg$/i', $_FILES['user_image']['type'])) 
            { 
                $ext = '.jpg'; 
            } 
            else if (preg_match('/^image\/gif$/i', $_FILES['user_image']['type']))
            { 
                $ext = '.gif'; 
            }
            else if (preg_match('/^image\/jpg$/i', $_FILES['user_image']['type']))
            { 
                $ext = '.jpg'; 
            } 
            else if (preg_match('/^image\/(x-)?png$/i', $_FILES['user_image']['type'])) 
            { 
                $ext = '.png'; 
            }
    
            $user_image = $user_image.$ext;
            $user_imagetoStore = './images/User_files/'.$user_image;
            copy($_FILES['user_image']['tmp_name'],$user_imagetoStore);
        }
        else
        {
            $user_image= "";
        }
    }
    

1 个答案:

答案 0 :(得分:0)

有几点想法:

  1. Objective-C代码使用名为userfile的字段,PHP代码段正在使用user_image

  2. 第二个PHP代码段不会生成任何JSON响应。

  3. 我也建议简单地说:

    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    

    这可能无关紧要,但当您拨打nil方法时,我也会使用Nil而不是POST

  4. 如果以上操作不起作用,我还建议在模拟器上运行此操作并在Charles(http://charlesproxy.com)中查看此内容并查看请求是否正常。