美文网首页
AFN上传图片 formdata格式

AFN上传图片 formdata格式

作者: 崔又淇 | 来源:发表于2016-11-08 14:04 被阅读107次

    + (void)imageAndContextUploadWithUrl:(NSString *)urlString        withParameters:(NSDictionary *)dic        withImageData:(NSArray *)imageArr          withFileName:(NSString *)fileName        uploadSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success          uploadError:(void (^)(AFHTTPRequestOperation *operation, NSError *error))uploadError {    

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];    manager.responseSerializer = [AFJSONResponseSerializer serializer];   

     AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];    policy.allowInvalidCertificates = YES;//允许自签证书   

     policy.validatesDomainName = NO;//检验域名    

    manager.securityPolicy = policy;//网络开启SSL协议   

     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain",@"application/atom+xml",@"application/xml",@"text/xml",nil];       

     [manager POST:urlString parameters:nil constructingBodyWithBlock:^(idformData) {

    for (NSInteger i = 0; i < imageArr.count; i++) {

    NSString *picName = [NSString stringWithFormat:@"onedayEvaluation%ld.%@", (long)i, @"jpg"];

    NSString *fileNameNew = [NSString stringWithFormat:@"%@%ld", fileName, (long)i];

    [formData appendPartWithFileData:imageArr[i]

    name:fileNameNew

    fileName:picName

    mimeType:@"image/jpeg"];

    }

    [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

    if ([key isEqualToString:@"orderId"]) {

    [formData appendPartWithFormData:[obj dataUsingEncoding:NSUTF8StringEncoding] name:@"order_id"];

    } else {

    [formData appendPartWithFormData:[obj dataUsingEncoding:NSUTF8StringEncoding] name:key];

    }

    }];

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

    // 成功回掉处理

    success(operation,responseObject);

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

    NSLog(@"error:%@",error);

    // 失败回掉处理

    uploadError(operation,error);

    }];

    }

    相关文章

      网友评论

          本文标题:AFN上传图片 formdata格式

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