从iOS发布到服务器的图像数组

时间:2015-01-07 06:57:29

标签: ios iphone nsurlrequest

我想将图像数组从iPhone上传到服务器,我应该如何将数组附加到NSMutableData。 这是我的代码:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://companyname.com/projectname/webservice.php?"]];
NSString *boundary = @"---------------------------239413274531762";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
NSMutableData *body = [NSMutableData data];

[request setHTTPMethod:@"POST"];
[request addValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

//TASK ---- companydetails
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"task\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[@"uploadmanuals" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// parameter1 --- userid
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:strUserID] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// parameter1 --- stockdict
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"productname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:productname] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


//Upload company logo image ......complogo
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:
                   @"Content-Disposition: form-data; name=\"uploadmanual\"; filename=\"%@\"\r\nContent-Type: image/jpeg\r\n\r\n",
                   @"logo.jpg"]dataUsingEncoding:NSUTF8StringEncoding]];

UIImage * imageToUpload = generatePhotoThumbnail(self.imgCompanyLogo.image);


if(imageToUpload)  {

    [body appendData:UIImageJPEGRepresentation(imageToUpload,1.0f)];
}
else {

    [body appendData:UIImageJPEGRepresentation(self.imgCompanyLogo.image,0.5f)];
}

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

我现在可以使用此发送单个图像,我想发送图像数组。

请帮忙。

0 个答案:

没有答案