我需要以数组形式将图像数据发送到服务器。我需要一个应该被服务器接受的示例代码。在服务器中,我们的后端开发人员接受以下格式{"images": {
"name": ["Desert.jpg", "Hydrangeas.jpg", "Koala.jpg", "Tulips.jpg"],
"type": ["image/jpeg", "image/jpeg", "image/jpeg", "image/jpeg"],
"tmp_name": ["/tmp/phpubHwme", "/tmp/phpUjyQBx", "/tmp/phpajU78R", "/tmp/phplOpTne"],
"error": [0, 0, 0, 0],
"size": [845941, 595284, 780831, 620888]
}}
我应该如何更改我的代码。我像下面这样发送
NSString *boundary = [self generateBoundaryString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",API_URL,path]]];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *httpBody=[NSMutableData data];
for (NSString *key in [paramDict allKeys]) {
[httpBody appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="%@";",key] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Type: text/plainrnrn"] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[paramDict valueForKey:key] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
}
NSInteger name=1;
for (NSData *data in images) {
[httpBody appendData:[[NSString stringWithFormat:@"--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="images"; filename="image%ld.jpg"rn",(long)name] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:[[NSString stringWithFormat:@"Content-Type: %@rnrn",[self mimeTypeForData:data]] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBody appendData:data];
[httpBody appendData:[@"rn" dataUsingEncoding:NSUTF8StringEncoding]];
name=name+1;
}
[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[httpBody appendData:[[NSString stringWithFormat:@"--%@--rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
如果我使用上述方法,它在服务器端显示为以下格式
{
images = {
error = 0;
name = "image2.jpg";
size = 87611;
"tmp_name" = "/tmp/phpO0LRJ2";
type = "image/jpeg";
};}
我应该以哪种格式发送图像?
NSArray *imgTypeArr = @[@"image/jpeg", @"image/jpeg", @"image/jpeg", @"image/jpeg"];
NSArray *nameArr = @[@"image/jpeg", @"image/jpeg", @"image/jpeg", @"image/jpeg"];
NSArray *sizeArr = @[[NSNumber numberWithInt:845941], [NSNumber numberWithInt:595284],[NSNumber numberWithInt:780831],[NSNumber numberWithInt:620888]];
NSArray *error = @[@"0",@"0",@"0",@"0"];
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"name",nameArr,@"type",imgTypeArr,@"error",error,@"size",sizeArr, nil];
NSArray *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"images",dict1,nil];
NSLog(@"%@",dict2);
- 创建这样的 NSDictionary 对象。
NSDictionary *bodyDictionary = @{ @"images" : @{ @"name" : @[@"Desert.jpg", @"Hydrangeas.jpg"], @"type" : @[@"image/jpeg", @"image/jpeg"], @"tmp_name" : @[@"tmp/phpubHwme", @"tmp/phpUjyQBx"], @"error" : @[@(0), @(0)], @"size" : @[@(845941), @(595284)] } }
- 将字典转换为数据
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:bodyDictionary options:NSJSONWritingPrettyPrinted error:&error];
[httpBody appendData:data dataUsingEncoding:NSUTF8StringEncoding]];
将数据转换为字符串检查创建的json
格式
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Json: %@", json);