将多个数据发送到服务器

时间:2014-01-08 16:24:09

标签: objective-c http-post nsurlconnection multipartform-data

我设法将一些带有图像数据的字符串发送到服务器。但是包含图像的数组有多个对象。我可以完美地从服务器获取第一张图片,但休息似乎消失了。有没有办法将多个数据文件发送到服务器,或者我的for循环发送图像有什么问题?服务器端代码似乎工作正常,因为它获取字符串和第一个图像文件,所以我认为在我的代码的objective-c部分必定有问题。

NSString *urlString = @"http://www.somesite.com/cgi-bin/somefile.py";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];

NSString *filenames = [NSString stringWithFormat:@"some string"]; //set name here
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"sessionString\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userEmail\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[sharedSingletonCenter.emailString dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

for (int i = 0; i < [self.pagesArray count]; i++) {
    NSData* imageData = UIImagePNGRepresentation(self.pagesArray[i]);
    NSLog(@"added %i", i+1);
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_%i\"; filename=\".png\"\r\n", i + 1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }

// setting the body of the post to the reqeust
[request setHTTPBody:body];

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"%@", returnString);
NSLog(@"finish");

1 个答案:

答案 0 :(得分:2)

问题是多部分表单元素用

分隔
[NSString stringWithFormat:@"\r\n--%@\r\n",boundary]

但随后以

结束
[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary]

但是你要在每个文件的末尾插入终止边界字符串。您希望更改代码以仅在最后执行此操作。因此:

NSMutableData *body = [NSMutableData data];

NSString *filenames = [NSString stringWithFormat:@"some string"]; //set name here
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"sessionString\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userEmail\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[sharedSingletonCenter.emailString dataUsingEncoding:NSUTF8StringEncoding]];

for (int i = 0; i < [self.pagesArray count]; i++) {
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSData* imageData = UIImagePNGRepresentation(self.pagesArray[i]);
    NSLog(@"added %i", i+1);
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_%i\"; filename=\".png\"\r\n", i + 1] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
}

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];