码迷,mamicode.com
首页 > Web开发 > 详细

AFNetworking上传文件

时间:2015-04-05 20:13:55      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

 

AFNetworking上传文件

标签:

原文地址:http://www.cnblogs.com/damiao/p/4394556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!