iOS之AFN文件上传(4)

AFN文件上传

  • 1.文件上传拼接数据的第一种方式
[formData appendPartWithFileData:data name:@"file" fileName:@"xxoo.png"mimeType:@"application/octet-stream"];
  • 2.文件上传拼接数据的第二种方式
 [formData appendPartWithFileURL:fileUrl name:@"file" fileName:@"xx.png" mimeType:@"application/octet-stream" error:nil];
  • 3.文件上传拼接数据的第三种方式
[formData appendPartWithFileURL:fileUrl name:@"file" error:nil];

文件上传相关的代码如

-(void)upload1
{
    //1.创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //2.处理参数(非文件参数)
    NSDictionary *dict = @{
                           @"username":@"123"
                           };

    //3.发送请求上传文件
    /*
     第一个参数:请求路径(1.NSString类型,2.不包含?号3.不包含参数)
     第二个参数:非文件参数,以字典的方式传递
     第三个参数:constructingBodyWithBlock 在该回调中拼接文件参数
     第四个参数:progress 进度回调
        uploadProgress.completedUnitCount:已经上传的数据大小
        uploadProgress.totalUnitCount:数据的总大小
     第五个参数:success 请求成功的回调
        task:上传Task
        responseObject:服务器返回的响应体信息(已经以JSON的方式转换为OC对象)
     第六个参数:failure 请求失败的回调
        task:上传Task
        error:错误信息
     */
    [manager POST:@"http://120.25.226.186:32812/upload" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        UIImage *image = [UIImage imageNamed:@"Snip20160117_1"];
        NSData *imageData = UIImagePNGRepresentation(image);

        //在该block中拼接要上传的文件参数
        /*
         第一个参数:要上传的文件二进制数据
         第二个参数:文件参数对应的参数名称,此处为file是该台服务器规定的(通常会在接口文档中提供)
         第三个参数:该文件上传到服务后以什么名称保存
         第四个参数:该文件的MIMeType类型
         */
        [formData appendPartWithFileData:imageData name:@"file" fileName:@"123.png" mimeType:@"image/png"];

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"请求成功----%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"请求失败----%@",error);
    }];
}
-(void)upload2
{
    //1.创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    //2.处理参数(非文件参数)
    NSDictionary *dict = @{
                           @"username":@"123"
                           };

    //3.发送请求上传文件
    /*
     第一个参数:请求路径(NSString类型)
     第二个参数:非文件参数,以字典的方式传递
     第三个参数:constructingBodyWithBlock 在该回调中拼接文件参数
     第四个参数:progress 进度回调
        uploadProgress.completedUnitCount:已经上传的数据大小
        uploadProgress.totalUnitCount:数据的总大小
     第五个参数:success 请求成功的回调
        task:上传Task
        responseObject:服务器返回的响应体信息(已经以JSON的方式转换为OC对象)
     第六个参数:failure 请求失败的回调
        task:上传Task
        error:错误信息
     */
    [manager POST:@"http://120.25.226.186:32812/upload" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        NSURL *fileUrl = [NSURL fileURLWithPath:@"/Users/OnlyChenJ/Desktop/Snip20160117_1.png"];


        //在该block中拼接要上传的文件参数
        //第一种拼接方法
        /*
         第一个参数:要上传的文件的URL路径
         第二个参数:文件参数对应的参数名称,此处为file是该台服务器规定的(通常会在接口文档中提供)
         第三个参数:该文件上传到服务后以什么名称保存
         第四个参数:该文件的MIMeType类型
         第五个参数:错误信息,传地址
         */
        //[formData appendPartWithFileURL:fileUrl name:@"file" fileName:@"1234.png" mimeType:@"image/png" error:nil];


        //第二种拼接方法:简写方法
        /*
         第一个参数:要上传的文件的URL路径
         第二个参数:文件参数对应的参数名称,此处为file
         第三个参数:错误信息
         说明:AFN内部自动获得路径URL地址的最后一个节点作为文件的名称,内部调用C语言的API获得文件的类型
         */
        [formData appendPartWithFileURL:fileUrl name:@"file" error:nil];

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"%f",1.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"请求成功----%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"请求失败----%@",error);
    }];
}
posted @ 2016-04-14 08:55  OnlyChenJ  阅读(393)  评论(0编辑  收藏  举报