Talha Ch Talha Ch - 17 days ago 8
iOS Question

Send NSString in AFMultipartFormData without using encoding

I'm uploading images and other media on server using

AFMultipartFormData
which was uploading fine. But the backend structure requires me to send a string value with every file that I upload.

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];


Now suppose
myString
is
@"0"
so the
NSASCIIStringEncoding
converts it to
<30>
but I just want
0
there. how to do that?

Answer

you can pass additional parameter with file as below :

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

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



            } error:&error];