在不使用编码的情况下在AFMultipartFormData中发送NSString

时间:2016-11-23 13:13:39

标签: ios objective-c encoding multipartform-data

我使用上传正常的AFMultipartFormData在服务器上上传图片和其他媒体。但是后端结构要求我为我上传的每个文件发送一个字符串值。

for(NSData *eachImage in dataStringArray) {

            NSString *mystring = [dataTypeArray objectAtIndex:[dataStringArray indexOfObject:eachImage]];

            NSLog(@"%@, %@",mystring,[mystring dataUsingEncoding:NSASCIIStringEncoding]);

            NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                [formData appendPartWithFormData:eachImage name:@"myImage"];
                [formData appendPartWithFormData:[mystring dataUsingEncoding:NSASCIIStringEncoding]
                                            name:@"FileDesc"];


            } error:&error];

现在假设myString@"0",因此NSASCIIStringEncoding会将其转换为<30>,但我只想要0。怎么做?

1 个答案:

答案 0 :(得分:3)

您可以使用文件传递其他参数,如下所示:

NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:@{@"FileDesc": mystring} parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                [formData appendPartWithFormData:eachImage name:@"myImage"];



            } error:&error];