博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POST jpeg upload with AFNetworking
阅读量:6921 次
发布时间:2019-06-27

本文共 5808 字,大约阅读时间需要 19 分钟。

NSData* sendData = [self.fileName.text dataUsingEncoding:NSUTF8StringEncoding];    NSDictionary *sendDictionary = [NSDictionary dictionaryWithObject:sendData forKey:@"name"];    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:remoteUrl];    NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"                                                                            path:@"/photos"                                                                      parameters:sendDictionary                                                       constructingBodyWithBlock:^(id 
formData) { [formData appendPartWithFileData:photoImageData name:self.fileName.text fileName:filePath mimeType:@"image/jpeg"]; } ]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest]; [operation setUploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) { NSLog(@"Sent %d of %d bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [operation setCompletionBlock:^{ NSLog(@"%@", operation.responseString); //Gives a very scary warning }]; [operation start];

 

+ (NSDictionary*)parametersOfUser:(User*)user{    if (user) {        NSMutableDictionary *returnDict = [NSMutableDictionary dictionaryWithCapacity:0];        if (user.userId && [user.userId length]) {            [returnDict setObject:[user.userId urlEncoded] forKey:@"userId"];        }                if (user.userName && [user.userName length]) {            [returnDict setObject:[user.userName urlEncoded] forKey:@"userName"];        }        if (user.phone && [user.phone length]) {            [returnDict setObject:[user.phone urlEncoded] forKey:@"phone"];        }        if (user.email && [user.email length]) {            [returnDict setObject:[user.email urlEncoded] forKey:@"email"];        }                [returnDict setObject:[user.deviceId urlEncoded] forKey:@"deviceId"];        [returnDict setObject:[user.deviceType urlEncoded] forKey:@"deviceType"];        [returnDict setObject:[user.osName urlEncoded] forKey:@"osName"];        [returnDict setObject:[user.osVersion urlEncoded] forKey:@"osVersion"];                if (user.pinCodeHash && [user.pinCodeHash length]) {            [returnDict setObject:[user.pinCodeHash urlEncoded] forKey:@"pinCodeHash"];        }        if (user.publicKey && [user.publicKey length]) {            [returnDict setObject:[user.publicKey urlEncoded] forKey:@"publicKey"];        }                return returnDict;    }return nil;}
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];NSMutableURLRequest *afRequest = [client multipartFormRequestWithMethod:@"POST"                                                                           path:path                                                                     parameters:[User parametersOfUser:user]                                                      constructingBodyWithBlock:^(id
formData) { /** *@discussion If we use multipart, we should only have two parts, one for picture (probably type is image/png) and one for other parameters (type is x-www-form-urlencoded) */ //header /* NSString *bodyString = [User postBodyStringWithUser:user withPostType:PostTypeMultiPart]; NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"application/x-www-form-urlencoded; charset=UTF-8",@"Content-Type", [NSString stringWithFormat:@"form-data; name=\"%@\"", @"usr_info"],@"Content-Disposition" , nil]; [formData appendPartWithHeaders:headers body:[bodyString dataUsingEncoding:NSUTF8StringEncoding]]; */ //picture part if (user.picture2) { NSData *data = UIImagePNGRepresentation(user.picture2); //NSLog(@"=====data length is %i",[data length]); [formData appendPartWithFileData:data name:@"picture2" fileName:nil mimeType:@"image/*"]; } }];

 

转载于:https://www.cnblogs.com/neozhu/archive/2013/01/19/2867569.html

你可能感兴趣的文章
freemarker常用知识总结
查看>>
css基础
查看>>
牛客第五场 G max 思维
查看>>
Javascript验证Textarea中是否有值/javascript验证select是否为空(是否选择)
查看>>
C语言中static关键字的作用
查看>>
maven依赖本地非repository中的jar包【转】
查看>>
JVM基础总结
查看>>
福大软工1816 · 第五次作业 - 结对作业2
查看>>
正则表达式基础
查看>>
android 让 EditText, TextView自动识别链接
查看>>
DLL编写教程
查看>>
使用ArrayList对大小写字母的随机打印
查看>>
快速理解URL重写
查看>>
IIS6 配置WCF REST服务注意事项
查看>>
【Android OpenGL ES】阅读hello-gl2代码(二)Java代码
查看>>
Fedora 7系统安装配置心得(转)(有图哦)
查看>>
Linux硬件信息查看(转)
查看>>
Android如何导入已有的外部数据库(在raw下自己导入db文件)
查看>>
MYSQL批处理
查看>>
证明hibernate一级缓存的存在
查看>>