zoukankan      html  css  js  c++  java
  • 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 ThinkUpload($config);// 实例化上传类
        $info = $upload -> upload();
       if($info){
        echo  json_encode(array('code' => 200,'data' => $info));
       }
       else
       {
          echo json_encode(array('code' => 500,'data' => $upload -> getError()));
       }
      }

  • 相关阅读:
    luogu P3804 【模板】后缀自动机 (SAM)
    莫队
    luogu P4688 [Ynoi2016]掉进兔子洞
    FZOJ 2331 LYK loves graph
    字典树
    luogu P6623 [省选联考 2020 A 卷] 树
    luogu P6018 [Ynoi2010]Fusion tree
    luogu P3264 [JLOI2015]管道连接
    最小斯坦纳树
    9. 回文数
  • 原文地址:https://www.cnblogs.com/kerul-weiwei/p/4354479.html
Copyright © 2011-2022 走看看