簡體   English   中英

使用AF網絡以多部分格式上傳圖像在iOS中不起作用

[英]Image upload in multipart format using AFnetworking not working in ios

  1. AFHTTPSessionManager用於從ios應用發布文件
  2. 服務器端使用php實現
  3. 如果文件是從Web服務器發送的,則服務器接受文件

我的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. Objective-C代碼使用名為userfile字段,PHP代碼段使用user_image

  2. 第二個PHP代碼段不生成任何JSON響應。

  3. 我還建議:

     sessionManager.responseSerializer = [AFJSONResponseSerializer serializer]; 

    可能沒關系,但是當您調用POST方法時,我也會使用nil而不是Nil

  4. 如果上述方法不起作用,我還建議您在模擬器上運行此程序,並在Charles( http://charlesproxy.com )中觀看此程序,看看請求是否正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM