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()));
   }
  }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。