您的位置:首页 > 理论基础 > 计算机网络

AFN网络框架2.x版—文件上传

2016-01-03 10:52 459 查看
/**
*  文件上传
*/
- (void)uploadFile
{
//1.获得请求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

//2.发送请求
//2.1封装请求参数(parameters:只能放非文件参数)
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"zhangsan";

[mgr POST:@"https://localhost/upload.php" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//文件上传在这个block里

//添加文件参数
NSString *file = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:file];

//上传
[formData appendPartWithFileData:data name:@"file" fileName:@"123.text" mimeType:@"text/plain"];

} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"失败");
}];

//非文件上传的POST请求
//    [mgr POST:<#(NSString *)#> parameters:<#(id)#> success:<#^(AFHTTPRequestOperation *operation, id responseObject)success#> failure:<#^(AFHTTPRequestOperation *operation, NSError *error)failure#>];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: