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

  • 相关阅读:
    Spring 让 LOB 数据操作变得简单易行
    让Apache Shiro保护你的应用
    MongoDB、Java及ORM
    Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
    Spring 的优秀工具类盘点,第 2 部分: 特殊字符转义和方法入参检测工具类
    SpringMVC:上传与下载
    Web数据挖掘在电子商务中的应用
    Mongodb快速入门之使用Java操作Mongodb
    Mongodb数据库入门之Spring Mongodb
    基于综合兴趣度的协同过滤推荐算法
  • 原文地址:https://www.cnblogs.com/kerul-weiwei/p/4354479.html
Copyright © 2020-2023  润新知