IOS-图片上传到服务器



//获取document 路径
- (NSString *)getDocumentPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return paths[0];

    }

/**
 * 发送图片
 */
- (void)sendImage
{
    
    //沙盒document的 路径
    NSString *documentPath = [self  getDocumentPath];

    //图片沙盒路径
    NSString *path = [NSString stringWithFormat:@"%@/1.png",documentPath];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    
    NSString *url = [NSString stringWithFormat:@"%@/user/upload",kHTTPPath];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        [formData appendPartWithFileData:data name:@"test1" fileName:@"test1.png" mimeType:@"image/png"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
      //  NSLog(@"YES %@",responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
       // NSLog(@"fail %@",error);
        
    }];  
}


php 接口

public function upload(){
   
   //上传图片
      $config = array(
    'maxSize' => 1024 * 1024,
    'rootPath' => './Home/',   //根目录
    'savePath' => 'Images/Post/', //图片文件夹目录
    'autoSub' => true,
    'saveName' => array('uniqid',''),
    'exts' => array('jpg', 'gif', 'png', 'jpeg'),
    'autoSub' => false,
    'subName' => array('date','Ymd'),
    );
    $upload = new \Think\Upload($config);// 实例化上传类
    $info = $upload -> upload();
   if($info){
    echo  json_encode(array('code' => 200,'data' => $info));
   }
   else
   {
      echo json_encode(array('code' => 500,'data' => $upload -> getError()));
   }
  }

posted @ 2015-03-20 20:16  Kerul.Wei  阅读(309)  评论(0编辑  收藏  举报