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

  • 相关阅读:
    window.location 对象所包含的属性
    控制器如何获取一条url中存在多个Id
    js 生成GUID
    JS控制前端控件的显示与隐藏
    ASP.NET CORE根据后台数值在razor页面进行判断单选按钮选中
    Selectize 控件使用以及js执行文件的时间差问题
    理解css中的position属性
    本地预览的vue项目,在githubpage静态展示
    关于img底部空白
    [优化]Steamroller-freecodecamp算法题目
  • 原文地址:https://www.cnblogs.com/kerul-weiwei/p/4354479.html
Copyright © 2011-2022 走看看