iOS 使用AFN 进行单图和多图上传
2015-12-03 11:15
381 查看
图片上传时必要将图片进行压缩,不然会上传失败
1.单张图上传
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
//使用日期生成图片名称
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
[formData appendPartWithFileData:imageData name:@"uploadFile" fileName:fileName mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
//上传图片成功执行回调
completion(responseObject,nil);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//上传图片失败执行回调
completion(nil,error);
}];
2.多图上传
多图上传和单图上传区别在于文件名称
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
NSInteger imgCount = 0;
for (NSData *imageData in imageDatas) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
NSString *fileName = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(imgCount)];
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"uploadFile%@",@(imgCount)] fileName:fileName mimeType:@"image/png"];
imgCount++;
}
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
completion(responseObject,nil);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
completion(nil,error);
}];
1.单张图上传
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
//使用日期生成图片名称
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *fileName = [NSString stringWithFormat:@"%@.png",[formatter stringFromDate:[NSDate date]]];
[formData appendPartWithFileData:imageData name:@"uploadFile" fileName:fileName mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
//上传图片成功执行回调
completion(responseObject,nil);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
//上传图片失败执行回调
completion(nil,error);
}];
2.多图上传
多图上传和单图上传区别在于文件名称
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:urlString parameters:params constructingBodyWithBlock:^(id_Nonnull formData) {
NSInteger imgCount = 0;
for (NSData *imageData in imageDatas) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS";
NSString *fileName = [NSString stringWithFormat:@"%@%@.png",[formatter stringFromDate:[NSDate date]],@(imgCount)];
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"uploadFile%@",@(imgCount)] fileName:fileName mimeType:@"image/png"];
imgCount++;
}
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
completion(responseObject,nil);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
completion(nil,error);
}];
相关文章推荐
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- iOS 开发笔记和技巧总结 (五)
- <iOS>关于Xcode上的Other linker flags
- iOS 类别中使用属性
- NBNS 数据包 和 NetBIOS 协议
- iOS——延展、类目、代理协议
- ios 发布账号过程
- iOS Developer Program
- iOS 开发笔记和技巧总结 (四)
- 登陆Demo--使用UserDeaults实现自动登录,传值,了解数据持久化方法
- iOS开发者账号以及各个账号的区别
- IOS-37-TableView及Cell的应用和设置详解
- iOS 开发笔记和技巧总结 (三)
- iOS NSLayoutAttributeLeft与NSLayoutAttributeLeading的区别
- IOS支付界面锁定按钮,防止支付第二次
- iOS 圆形按钮(或图片)
- iOS 开发 Pch 文件的正确使用
- [iOS进阶]Baidu Map SDK之批量请求
- ios Crash闪退日志获取和上传至服务器
- iOS TableView经常用得到