美文网首页网络相关
iOS使用AFNetworking上传图片或者文件

iOS使用AFNetworking上传图片或者文件

作者: 饭_米粒 | 来源:发表于2015-11-16 17:08 被阅读6044次

AFHTTPRequestOperationManager *�manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer= [AFHTTPResponseSerializer serializer];

[manager POST:url parameters:params constructingBodyWithBlock:^(id formData) {

NSData *data =UIImageJPEGRepresentation(image,0.5);//把要上传的图片转成NSData

//把要上传的文件转成NSData

//NSString*path=[[NSBundlemainBundle]pathForResource:@"123"ofType:@"txt"];

//NSData*fileData = [NSDatadataWithContentsOfFile:path];

[formData appendPartWithFileData:dataname:@"uploadFile" fileName:@"图片1" mimeType:@"image/png"];//给定数据流的数据名,文件名,文件类型(以图片为例)

/*常用数据流类型:

@"image/png" 图片

 @“video/quicktime” 视频流

*/

}success:^(AFHTTPRequestOperation*operation,id responseObject) {

//NSLog(@"%@",responseObject);//返回结果

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

//NSLog(@"%@",error.localizedDescription);//请求失败结果

}];

相关文章

网友评论

  • ec8c5ebc00d2:现在AFN更新了,没有这个类了啊AFHTTPRequestOperationManager,怎么请求啊
    15df6e15f6c1:上传其他文件怎么搞啊,如果不知道是doc还是pdf呢
    0f4ac6f61ff0:@董君君君子 用AFHTTPSessionManager
  • 6d4da852cebe:没有demo的都不是好文章,, 邮箱 :942147529@qq 谢谢

本文标题:iOS使用AFNetworking上传图片或者文件

本文链接:https://www.haomeiwen.com/subject/febkhttx.html