AFNetworking上传文件

-(void)upload{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"deviceName":@"imageName",@"deviceId":@"1",@"deviceChildId":@"11"};
    [manager POST:@"http://127.0.0.1:8080/upload" parameters:params
                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
         
        UIImage *image = [UIImage imageNamed:@"a.jpg"];
        NSData *imageData = UIImageJPEGRepresentation(image, 1);
         
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
         
        // 上传图片,以文件流的格式
        [formData appendPartWithFileData:imageData name:@"deviceImg"
                                fileName:fileName mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
         
        NSLog(@"上传成功返回结果 : %@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error::::%@",error);
    }];
}

 

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