iOS 文件流 传NSData类型数据给.net后台 传NSData类型图片给后台

首先第一次跟.net后台配合,一个很简单的传图片给后台

之前都是java后台, 要求传图片都是 将图片转String类型,然后拼接参数穿给后台,这个没啥说的

这次后台要直接传NSData类型,用了如下方法,后台说接收到了但是数据不对,让我去掉name,filename, mimeType这三个字段,于是继续找别的方法:

+ (void)uploadHeadImage:(NSData *)imageData back:(void(^)(NSDictionary * dic))block
{
    NSString *headUrl = [NSString stringWithFormat:@"%@%@",CommonUrl,UploadHeadImage];
    NSDictionary *params = @{@"userId":UserInfo(User_user_id)};
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        [manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html",@"application/json",@"image/png",nil];
        [manager POST:headUrl parameters:params constructingBodyWithBlock:^(id  _Nonnull formData) {
            [formData appendPartWithFileData:imageData
                                        name:@"file"
                                    fileName:@"file.png"
                                    mimeType:@"png"];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            //            NSLog(@"%@",uploadProgress);
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            block(responseObject);
            if (responseObject && [responseObject[@"status"] isEqualToString:@"1"]) {
                
            }else {
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
        }];
    });
    
}

下面是我最后解决的方法, 在网上没有找到类似的, 所以贴出来,粘贴就可以用,原谅我的学艺不精

  NSString *urlStr = [NSString stringWithFormat:@"你的拼接好的URL"];
    NSLog(@"打印输出URL===%@", urlStr);
  NSString *resultUrl = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:resultUrl];
    
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:1 timeoutInterval:2.0f];
    request.HTTPMethod=@"POST";//设置请求方法是POST
    request.timeoutInterval=15.0;//设置请求超时
//    NSData *imageData = UIImagePNGRepresentation(self.photoImage);
    

    NSData *imageData = UIImageJPEGRepresentation(imageV, 0.5);
    //声明myRequestData,用来放入http body
    NSMutableData *myRequestData=[NSMutableData data];
    
    //将image的data加入
    [myRequestData appendData:imageData];
    
    //设置HTTPHeader中Content-Type的值
    NSString *content=[[NSString alloc]initWithFormat:@"text?;"];
    
    //设置HTTPHeader
    [request setValue:content forHTTPHeaderField:@"Content-Type"];
    
    //设置Content-Length
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
    
    //设置http body
    [request setHTTPBody:myRequestData];
    
    NSURLSession *session=[NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"post==%@",result);
       
        
        NSString *tips = result[@"tips"];
        if ([tips isEqualToString:@"上传成功"]) {
   
   
        }
  
    }];
 

你可能感兴趣的:(iOS 文件流 传NSData类型数据给.net后台 传NSData类型图片给后台)